Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flaskapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
db = SQLAlchemy(app)
app.app_context().push()

from flaskapp import routes
from flaskapp import routes
65 changes: 63 additions & 2 deletions flaskapp/routes.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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/<country>')
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)
15 changes: 15 additions & 0 deletions flaskapp/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion flaskapp/templates/about.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "layout.html" %}
{% block content %}
<h1>{{ title }}</h1>
<p>This is where I'll write something about myself.</p>
<p>Anzhelika Belozerova, MDS student.</p>
{% endblock %}
15 changes: 15 additions & 0 deletions flaskapp/templates/heatmap_voting_patterns.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="plot"></div>
<script>
var graphData = {{ graphJSON | safe }};
Plotly.newPlot('plot', graphData.data, graphData.layout);
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions flaskapp/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ <h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
</div>
{% endfor %}

<div class="annotation">
<p class="annotation-text">GB data analysis.</p>
</div>
{% endblock %}
2 changes: 2 additions & 0 deletions flaskapp/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ <h1 class="logo">My web app</h1>
<li><a href="{{ url_for('about') }}">About</a></li>
<li><a href="{{ url_for('new_post') }}">New post</a></li>
<li><a href="{{ url_for('dashboard') }}">Dashboard</a></li>
<li><a href="{{ url_for('heatmap_voting_patterns', country='England') }}">Correlation</a></li>
<li><a href="{{ url_for('student_labour_votes_scatter') }}">Students vs. Labor</a></li>
</ul>
</nav>
</strong>
Expand Down
55 changes: 55 additions & 0 deletions flaskapp/templates/student_labour_votes_scatter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% extends "layout.html" %}
{% block content %}
<h1>{{ title }}</h1>
<div id='student-labour-votes-scatter' class='chart'></div>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<script type='text/javascript'>
var scatterChartData = {{ graphJSON | safe }};
var scatterChartLayout = {
title: {
text: 'Percentage of Full-time Students vs. Labour Party Votes in UK Constituencies',
font: {
size: 36,
color: 'rgb(31, 119, 180)'
}
},
xaxis: {
title: 'Percentage of Full-time Students',
tickfont: {
size: 14,
color: 'rgba(0,0,0,0.7)'
}
},
yaxis: {
title: 'Labour Party Votes',
tickfont: {
size: 14,
color: 'rgba(0,0,0,0.7)'
}
},
margin: {
l: 100,
r: 100,
b: 100,
t: 100,
pad: 4
},
autosize: false,
width: 1200,
height: 800,
plot_bgcolor: 'rgb(255, 255, 255)',
paper_bgcolor: 'rgb(255, 255, 255)',
mode: 'markers',
marker: {
size: 10,
opacity: 0.7,
color: 'rgb(214, 39, 40)',
line: {
width: 1,
color: 'rgb(51, 51, 51)'
}
}
};
Plotly.newPlot('student-labour-votes-scatter', scatterChartData, scatterChartLayout);
</script>
{% endblock %}
Binary file modified instance/site.db
Binary file not shown.
1 change: 1 addition & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

if __name__ == '__main__':
app.run(debug=True, port=5005)