Commit 5832ab4f authored by Christophe Benz's avatar Christophe Benz

Use mapbox

parent f14dd771
MAPBOX_TOKEN=
.mypy_cache/
__pycache__/
.env
......@@ -8,6 +8,7 @@ import dash_html_components as html
import flask
import plotly.express as px
from dash.dependencies import Input, Output
from dotenv import load_dotenv
import app_data
......@@ -24,6 +25,8 @@ COLORS = [
"rgba(23,190,207,1)",
]
color_discrete_map = dict(zip(app_data.COLL_TYPES, COLORS))
def none_if_empty(val):
"""Return None if empty else original value."""
......@@ -39,8 +42,11 @@ def compute_dept_dropdown_options(reg_code=None):
return [{"label": "-- département --", "value": ""}] + dep_list
load_dotenv()
server = flask.Flask(__name__)
server.secret_key = os.environ.get("secret_key", str(randint(0, 1000000)))
px.set_mapbox_access_token(os.getenv("MAPBOX_TOKEN"))
app = dash.Dash(__name__, server=server)
app.layout = html.Div(
children=[
......@@ -100,10 +106,15 @@ def update_type_bar(reg_code, dep_code):
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}}]
}
df = df.groupby("type").size()
return px.bar(
df,
x=df.index,
y=df.values,
labels={"x": "Type de collectivité", "y": "Nombre"},
color=df.index,
color_discrete_map=color_discrete_map,
)
@app.callback(
......@@ -116,14 +127,13 @@ def update_type_bar(reg_code, dep_code):
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(
return px.scatter_mapbox(
df,
lat="lat",
lon="long",
color="type",
hover_name="nom",
color_discrete_map=dict(zip(app_data.COLL_TYPES, COLORS)),
projection="natural earth",
color_discrete_map=color_discrete_map,
)
......
......@@ -56,11 +56,3 @@ def compute_dep_list(reg_code=None):
else:
# Filter on region code
return [(d[1], d[2]) for d in reg_dep_list if d[0] == reg_code]
def compute_charts_data(df):
"""Compute (values, labels) from given dataframe."""
counts_data = df.groupby("type").agg("count").siren
counts_map = dict(zip(counts_data.index.tolist(), counts_data.values.tolist()))
return [counts_map.get(lo, 0) for lo in COLL_TYPES], COLL_TYPES
......@@ -2,3 +2,4 @@ dash
gunicorn
pandas
plotly_express
python-dotenv
......@@ -23,6 +23,7 @@ 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
python-dotenv==0.10.3
pytz==2019.3 # via pandas
retrying==1.3.3 # via plotly
scipy==1.4.1 # via plotly-express, statsmodels
......
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