-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6efdc3b
commit 1068514
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |