-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
65 lines (51 loc) · 1.63 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pandas as pd
from pymongo import MongoClient
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
import plotly.express as px
# Connect to MongoDB
client = MongoClient("mongodb://root:rootpassword@localhost:27017")
db = client["food_db"]
collection = db["food_analyze"]
# Read data from MongoDB and convert to DataFrame
data = list(collection.find())
df = pd.DataFrame(data)
# Create a Dash app
app = dash.Dash(__name__)
# Define app layout
app.layout = html.Div([
html.H1("Food Data Visualization"),
dcc.Checklist(
id="gender-checklist",
options=[
{"label": "Female", "value": "Female"},
{"label": "Male", "value": "Male"}
],
value=["Female", "Male"]
),
dcc.Graph(id="histogram-graph"),
dcc.Graph(id="pie-chart"),
])
# Create a callback to update the histogram chart
@app.callback(
Output("histogram-graph", "figure"),
[Input("gender-checklist", "value")]
)
def update_histogram_chart(selected_genders):
filtered_df = df[df["GENDER_DESC"].isin(selected_genders)]
fig = px.histogram(filtered_df, x="WEIGHT", color="GENDER_DESC")
return fig
# Create a callback to update the pie chart
@app.callback(
Output("pie-chart", "figure"),
[Input("gender-checklist", "value")]
)
def update_pie_chart(selected_genders):
filtered_df = df[df["GENDER_DESC"].isin(selected_genders)]
fig = px.pie(filtered_df, names="CALORIES_DAY", title="Calories Day")
return fig
# Run the app
if __name__ == "__main__":
app.run_server()