diff --git a/flaskapp/__init__.py b/flaskapp/__init__.py index d099ba574..d6882897c 100644 --- a/flaskapp/__init__.py +++ b/flaskapp/__init__.py @@ -12,4 +12,4 @@ db = SQLAlchemy(app) app.app_context().push() -from flaskapp import routes +from flaskapp import routes \ No newline at end of file diff --git a/flaskapp/routes.py b/flaskapp/routes.py index ac7f7f5f0..d04e6c538 100644 --- a/flaskapp/routes.py +++ b/flaskapp/routes.py @@ -1,13 +1,16 @@ from flask import render_template, flash, redirect, url_for, request from flaskapp import app, db -from flaskapp.models import BlogPost, IpView, Day +from flaskapp.models import BlogPost, IpView, Day, UkData from flaskapp.forms import PostForm import datetime - +import matplotlib.pyplot as plt import pandas as pd +import seaborn as sns import json import plotly import plotly.express as px +import numpy as np + # Route for the home page, which is where the blog posts will be shown @@ -71,3 +74,61 @@ def before_request_func(): db.session.add(ip_view) # insert into the ip_view table db.session.commit() # commit all the changes to the database + + +@app.route('/student-labour-votes-scatter') +def student_labour_votes_scatter(): + data = UkData.query.with_entities(UkData.constituency_name, UkData.c11FulltimeStudent, UkData.LabVote19).all() + df = pd.DataFrame(data, columns=['Constituency', 'Full-time Student (%)', 'Labour Party Votes']) + fig = px.scatter(df, x='Full-time Student (%)', y='Labour Party Votes', title='Percentage of Full-time Students vs. Labour Party Votes in UK Constituencies', color='Labour Party Votes', color_continuous_scale='RdYlBu') + fig.update_xaxes(title='Percentage of Full-time Students', tickfont=dict(size=14), title_font=dict(size=18)) + fig.update_yaxes(title='Labour Party Votes', tickfont=dict(size=14), title_font=dict(size=18)) + fig.update_layout( + margin=dict(l=50, r=50, t=100, b=100), + autosize=False, + width=1500, + height=800, + plot_bgcolor='rgba(240, 240, 240, 0.9)', + paper_bgcolor='rgba(240, 240, 240, 0.9)', + font=dict(size=16), + ) + graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) + return render_template('student_labour_votes_scatter.html', title='Percentage of Full-time Students vs. Labour Party Votes', graphJSON=graphJSON) + + +@app.route('/heatmap_voting_patterns/') +def heatmap_voting_patterns(country): + + data = UkData.query.filter_by(country=country).all() + + df = pd.DataFrame([{ + 'Constituency': d.constituency_name, + 'Region': d.region, + 'Turnout 2019': d.Turnout19, + 'Conservative Votes': d.ConVote19, + 'Labour Votes': d.LabVote19, + 'Lib Dem Votes': d.LDVote19, + 'SNP Votes': d.SNPVote19, + 'Plaid Cymru Votes': d.PCVote19, + 'UKIP Votes': d.UKIPVote19, + 'Green Votes': d.GreenVote19, + 'Brexit Votes': d.BrexitVote19, + 'Population Density': d.c11PopulationDensity, + 'Percentage Retired': d.c11Retired + } for d in data]) + + numeric_df = df.select_dtypes(include=[np.number]) # This filters out non-numeric columns + correlation_matrix = numeric_df.corr() + fig = px.imshow(correlation_matrix, + labels=dict(x="Variable", y="Variable", color="Correlation"), + x=correlation_matrix.columns, + y=correlation_matrix.columns, + title=f"Correlation Heatmap of Voting Patterns and Demographics in {country}") + + + fig.update_xaxes(side="bottom") + graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) + + return render_template('heatmap_voting_patterns.html', + title=f'Correlation Heatmap of Voting Patterns and Demographics in {country}', + graphJSON=graphJSON) \ No newline at end of file diff --git a/flaskapp/static/main.css b/flaskapp/static/main.css index 82afa3a62..b68ba1bf8 100644 --- a/flaskapp/static/main.css +++ b/flaskapp/static/main.css @@ -85,3 +85,18 @@ header { .article-content { color: #555; } + +/* Additional styles for annotation */ +.annotation { + margin-top: 40px; + padding: 20px; + background-color: #f5f5f5; + border-radius: 5px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); +} + +.annotation-text { + font-size: 16px; + line-height: 1.6; + color: #333; +} \ No newline at end of file diff --git a/flaskapp/templates/about.html b/flaskapp/templates/about.html index c7da59267..b028ad5b2 100644 --- a/flaskapp/templates/about.html +++ b/flaskapp/templates/about.html @@ -1,5 +1,5 @@ {% extends "layout.html" %} {% block content %}

{{ title }}

-

This is where I'll write something about myself.

+

Anzhelika Belozerova, MDS student.

{% endblock %} diff --git a/flaskapp/templates/heatmap_voting_patterns.html b/flaskapp/templates/heatmap_voting_patterns.html new file mode 100644 index 000000000..4c028b59f --- /dev/null +++ b/flaskapp/templates/heatmap_voting_patterns.html @@ -0,0 +1,15 @@ + + + + + {{ title }} + + + +
+ + + diff --git a/flaskapp/templates/home.html b/flaskapp/templates/home.html index 8b0b1c41b..45637f408 100644 --- a/flaskapp/templates/home.html +++ b/flaskapp/templates/home.html @@ -7,4 +7,8 @@

{{ post.title }}

{{ post.content }}

{% endfor %} + +
+

GB data analysis.

+
{% endblock %} \ No newline at end of file diff --git a/flaskapp/templates/layout.html b/flaskapp/templates/layout.html index 5c525292d..2e918db11 100644 --- a/flaskapp/templates/layout.html +++ b/flaskapp/templates/layout.html @@ -16,6 +16,8 @@

My web app

  • About
  • New post
  • Dashboard
  • +
  • Correlation
  • +
  • Students vs. Labor
  • diff --git a/flaskapp/templates/student_labour_votes_scatter.html b/flaskapp/templates/student_labour_votes_scatter.html new file mode 100644 index 000000000..b46ab5f40 --- /dev/null +++ b/flaskapp/templates/student_labour_votes_scatter.html @@ -0,0 +1,55 @@ +{% extends "layout.html" %} +{% block content %} +

    {{ title }}

    +
    + + +{% endblock %} diff --git a/instance/site.db b/instance/site.db index 1f77ee429..54ac789cb 100644 Binary files a/instance/site.db and b/instance/site.db differ diff --git a/run.py b/run.py index 506bac879..b9658ebab 100644 --- a/run.py +++ b/run.py @@ -2,3 +2,4 @@ if __name__ == '__main__': app.run(debug=True, port=5005) +