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
76 changes: 76 additions & 0 deletions flaskapp/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import plotly
import plotly.express as px

from .models import UkData
import plotly.graph_objs as go



# Route for the home page, which is where the blog posts will be shown
@app.route("/")
Expand Down Expand Up @@ -71,3 +75,75 @@ 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_engagement')
def student_engagement():
# Assuming UkData model and necessary imports are defined
data = UkData.query.with_entities(
UkData.constituency_name,
UkData.c11FulltimeStudent,
UkData.Turnout19
).all()

df = pd.DataFrame(data, columns=['Constituency', 'Percentage of Full-Time Students', 'Voter Turnout 2019'])
# Create a scatter plot using Plotly with a vibrant, yet elegant color scheme
fig = px.scatter(df, x='Percentage of Full-Time Students', y='Voter Turnout 2019',
hover_data=['Constituency'], title='Impact of Full-Time Student Population on Voter Turnout',
color_continuous_scale=px.colors.sequential.Magma) # Using Viridis color scale

# Customize the appearance
fig.update_layout(
xaxis_title="Percentage of Full-Time Students",
yaxis_title="Voter Turnout (%)",
plot_bgcolor="white",
paper_bgcolor="lightgrey" # Adding a subtle background color to the plot area
)
fig.update_traces(marker=dict(size=12,
line=dict(width=2,
color='DarkSlateGrey')),
selector=dict(mode='markers'))

# Convert the figure to JSON
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)

# Pass the JSON to the HTML template
return render_template('student_engagement.html', title='Student Engagement Analysis', graphJSON=graphJSON)
@app.route('/student_labour_support')
def student_labour_support():
# Fetch data
data = UkData.query.with_entities(
UkData.constituency_name,
UkData.c11FulltimeStudent,
UkData.LabVote19,
UkData.TotalVote19
).all()
df = pd.DataFrame(data, columns=['Constituency', 'Percentage of Full-Time Students', 'Labour Votes in 2019', 'Total Votes 2019'])
df['Labour Vote Percentage'] = (df['Labour Votes in 2019'] / df['Total Votes 2019']) * 100

# Create a scatter plot with manual colors and sizes
df['Color'] = df['Labour Vote Percentage'].apply(lambda x: 'red' if x > 50 else 'blue')
df['Size'] = df['Labour Vote Percentage'].apply(lambda x: 20 if x > 50 else 10)

fig = go.Figure(data=[go.Scatter(
x=df['Percentage of Full-Time Students'],
y=df['Labour Vote Percentage'],
text=df['Constituency'],
mode='markers',
marker=dict(
color=df['Color'], # Custom colors
size=df['Size'] # Custom sizes
)
)])

fig.update_layout(
title='Full-Time Student Percentage vs Labour Vote Percentage in 2019',
xaxis_title="Percentage of Full-Time Students",
yaxis_title="Labour Vote Percentage",
plot_bgcolor="white"
)

# Convert the figure to JSON
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)

# Pass the JSON to the HTML template
return render_template('student_labour_support.html', title='Custom Student and Labour Support Analysis', graphJSON=graphJSON)
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('student_engagement') }}">Student Engagement</a> </li>
<li><a href="{{ url_for('student_labour_support') }}">Student Labour Support</a> </li>
</ul>
</nav>
</strong>
Expand Down
10 changes: 10 additions & 0 deletions flaskapp/templates/student_engagement.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "layout.html" %}
{% block content %}
<h1>{{ title }}</h1>
<div id='chart' style="width:100%; height:100%;"></div>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<script type='text/javascript'>
var graphs = {{ graphJSON | safe }};
Plotly.newPlot('chart', graphs.data, graphs.layout);
</script>
{% endblock %}
10 changes: 10 additions & 0 deletions flaskapp/templates/student_labour_support.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "layout.html" %}
{% block content %}
<h1>{{ title }}</h1>
<div id='chart' style="width:100%; height:100%;"></div>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<script type='text/javascript'>
var graphs = {{ graphJSON | safe }};
Plotly.newPlot('chart', graphs.data, graphs.layout);
</script>
{% endblock %}
Binary file modified instance/site.db
Binary file not shown.
28 changes: 28 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
blinker==1.8.2
click==8.1.7
contourpy==1.2.1
cycler==0.12.1
Flask==3.0.3
Flask-SQLAlchemy==3.1.1
Flask-WTF==1.2.1
fonttools==4.51.0
itsdangerous==2.2.0
Jinja2==3.1.4
kiwisolver==1.4.5
MarkupSafe==2.1.5
matplotlib==3.8.4
numpy==1.26.4
packaging==24.0
pandas==2.2.2
pillow==10.3.0
plotly==5.22.0
pyparsing==3.1.2
python-dateutil==2.9.0.post0
pytz==2024.1
six==1.16.0
SQLAlchemy==2.0.30
tenacity==8.3.0
typing_extensions==4.11.0
tzdata==2024.1
Werkzeug==3.0.3
WTForms==3.1.2