Commit f14dd771 authored by Christophe Benz's avatar Christophe Benz

Add map

parent 8523afc8
......@@ -6,6 +6,7 @@ import dash
import dash_core_components as dcc
import dash_html_components as html
import flask
import plotly.express as px
from dash.dependencies import Input, Output
import app_data
......@@ -38,22 +39,6 @@ def compute_dept_dropdown_options(reg_code=None):
return [{"label": "-- département --", "value": ""}] + dep_list
def compute_bar_figure(reg_code=None, dep_code=None):
"""Build a bar figure from given region and department code."""
df = app_data.df
if dep_code:
df = df[df["dep_code"] == dep_code]
elif reg_code:
df = df[df["reg_code"] == reg_code]
values, labels = app_data.compute_charts_data(df)
return {
"data": [{"x": labels, "y": values, "type": "bar", "marker": {"color": COLORS}}]
}
server = flask.Flask(__name__)
server.secret_key = os.environ.get("secret_key", str(randint(0, 1000000)))
app = dash.Dash(__name__, server=server)
......@@ -68,28 +53,38 @@ app.layout = html.Div(
],
value="",
),
dcc.Dropdown(id="dep_dd", options=compute_dept_dropdown_options(), value="",),
dcc.Graph(id="type_bar", figure=compute_bar_figure()),
dcc.Dropdown(id="dep_dd", options=compute_dept_dropdown_options(), value=""),
dcc.Graph(id="type_bar"),
dcc.Graph(id="map"),
]
)
@app.callback(
Output(component_id="dep_dd", component_property="options"),
[
Output(component_id="dep_dd", component_property="options"),
Output(component_id="dep_dd", component_property="value"),
],
[Input(component_id="reg_dd", component_property="value")],
)
def update_dept_dropdown_options(reg_code):
"""Update department list when a region has been chosen."""
return compute_dept_dropdown_options(reg_code=none_if_empty(reg_code))
options = compute_dept_dropdown_options(reg_code=none_if_empty(reg_code))
value = ""
return options, value
@app.callback(
Output(component_id="dep_dd", component_property="value"),
[Input(component_id="reg_dd", component_property="value")],
)
def update_dep_dropdown_value(reg_code):
"""Update department list when a region has been chosen."""
return ""
def filter_df(df, reg_code, dep_code):
"""Filter dataframe by applying region code and department code."""
reg_code = none_if_empty(reg_code)
dep_code = none_if_empty(dep_code)
if dep_code:
df = df[df["dep_code"] == dep_code]
elif reg_code:
df = df[df["reg_code"] == reg_code]
return df
@app.callback(
......@@ -100,9 +95,35 @@ def update_dep_dropdown_value(reg_code):
],
)
def update_type_bar(reg_code, dep_code):
"""Update bar chart when one dropdown value changes."""
return compute_bar_figure(
reg_code=none_if_empty(reg_code), dep_code=none_if_empty(dep_code)
"""Update bar chart when one dropdown value changes.
Build a bar figure from given region and department code.
"""
df = filter_df(app_data.df, reg_code, dep_code)
values, labels = app_data.compute_charts_data(df)
return {
"data": [{"x": labels, "y": values, "type": "bar", "marker": {"color": COLORS}}]
}
@app.callback(
Output(component_id="map", component_property="figure"),
[
Input(component_id="reg_dd", component_property="value"),
Input(component_id="dep_dd", component_property="value"),
],
)
def update_map(reg_code, dep_code):
"""Update map chart when one dropdown value changes."""
df = filter_df(app_data.df, reg_code, dep_code)
return px.scatter_geo(
df,
lat="lat",
lon="long",
color="type",
hover_name="nom",
color_discrete_map=dict(zip(app_data.COLL_TYPES, COLORS)),
projection="natural earth",
)
......
dash
gunicorn
pandas
plotly_express
......@@ -17,13 +17,17 @@ gunicorn==20.0.4
itsdangerous==1.1.0 # via flask
jinja2==2.10.3 # via flask
markupsafe==1.1.1 # via jinja2
numpy==1.18.1 # via pandas
numpy==1.18.1 # via pandas, patsy, plotly-express, scipy, statsmodels
pandas==0.25.3
plotly==4.4.1 # via dash
patsy==0.5.1 # via plotly-express, statsmodels
plotly-express==0.4.1
plotly==4.4.1 # via dash, plotly-express
python-dateutil==2.8.1 # via pandas
pytz==2019.3 # via pandas
retrying==1.3.3 # via plotly
six==1.13.0 # via plotly, python-dateutil, retrying
scipy==1.4.1 # via plotly-express, statsmodels
six==1.13.0 # via patsy, plotly, python-dateutil, retrying
statsmodels==0.10.2 # via plotly-express
werkzeug==0.16.0 # via flask
# The following packages are considered to be unsafe in a requirements file:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment