Skip to content

Commit

Permalink
updated python_flask
Browse files Browse the repository at this point in the history
  • Loading branch information
Helzheng123 committed Sep 26, 2023
1 parent 6efdc3b commit 1068514
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
47 changes: 47 additions & 0 deletions python_flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from flask import Flask, render_template, request
import pandas as pd
import matplotlib
matplotlib.use('Agg') # required for local development and g-shell
import matplotlib.pyplot as plt
import io
import base64

import warnings
warnings.simplefilter("ignore", UserWarning)

app = Flask(__name__)

# Load the dataset
url = "https://raw.githubusercontent.com/Helzheng123/datasci_4_web_viz/main/dataset/PLACES__Local_Data_for_Better_Health__County_Data_2023_release.csv"
df = pd.read_csv(url)
df_diabetes = df[(df['MeasureId'] == 'DIABETES') & (df['Data_Value_Type'] == 'Age-adjusted prevalence')]

@app.route('/', methods=['GET', 'POST'])
def index():
counties = sorted(df_diabetes['LocationName'].unique())
selected_county = request.form.get('county') or counties[0]

img = create_plot(selected_county)

return render_template("index.html", counties=counties, selected_county=selected_county, img=img)

def create_plot(county):
overall_avg = df_diabetes['Data_Value'].mean()
selected_county_avg = df_diabetes[df_diabetes['LocationName'] == county]['Data_Value'].mean()

fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(['Selected County', 'Overall Average'], [selected_county_avg, overall_avg], color=['orchid', 'darksalmon'])
ax.axhline(selected_county_avg, color='gray', linestyle='dashed', alpha=0.7)
ax.set_ylabel('Data Value (Age Adjusted prevalence) - Percent')
ax.set_ylim(0, 30)
ax.set_title('Diagnosed Diabetes Age Adjusted Prevalence Comparison')

# Convert plot to PNG image
img = io.BytesIO()
plt.savefig(img, format='png')
img.seek(0)

return base64.b64encode(img.getvalue()).decode()

if __name__ == '__main__':
app.run(debug=True)
21 changes: 21 additions & 0 deletions python_flask/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Flask Diabetes App</title>
</head>
<body>

<h2>Diagnosed Diabetes Prevalence in Connecticut by County</h2>

<form method="post">
<select name="county" onchange="this.form.submit()">
{% for county in counties %}
<option value="{{ county }}" {% if county == selected_county %} selected {% endif %}>{{ county }}</option>
{% endfor %}
</select>
</form>

<img src="data:image/png;base64,{{ img }}" alt="Diabetes Prevalance Visualization" />

</body>
</html>

0 comments on commit 1068514

Please sign in to comment.