From b67bf183f8086a8cf82ee436ba6ed0d1a11c4926 Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Sat, 29 Jan 2022 19:45:10 +0100 Subject: [PATCH] Restructured app for multiview, first working callback --- stats/app.py | 6 +++++ stats/index.py | 30 +++++++++++++++++++++ stats/views/__init__.py | 0 dash/app.py => stats/views/siret.py | 41 +++++++++++------------------ 4 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 stats/app.py create mode 100644 stats/index.py create mode 100644 stats/views/__init__.py rename dash/app.py => stats/views/siret.py (74%) diff --git a/stats/app.py b/stats/app.py new file mode 100644 index 0000000..9df8491 --- /dev/null +++ b/stats/app.py @@ -0,0 +1,6 @@ +import dash +import dash_bootstrap_components as dbc + +app = dash.Dash("trackdechets-public-dash", title='DECP.info : statistiques', + external_stylesheets=[dbc.themes.GRID], suppress_callback_exceptions=True) +server = app.server diff --git a/stats/index.py b/stats/index.py new file mode 100644 index 0000000..c38589b --- /dev/null +++ b/stats/index.py @@ -0,0 +1,30 @@ +from dash import html, dcc +import plotly.io as pio +from os import getenv +from app import app +from dash.dependencies import Input, Output +from views import siret + +pio.templates.default = "none" + + +app.layout = html.Div([ + dcc.Location(id='url', refresh=False), + html.Div(id='page-content') +]) + + +@app.callback(Output('page-content', 'children'), + Input('url', 'pathname')) +def display_page(pathname): + if pathname.startswith('/siret/'): + return siret.layout + else: + return '404' + + +if __name__ == '__main__': + port = getenv('PORT', 8050) + + # Scalingo requires 0.0.0.0 as host, instead of the default 127.0.0.1 + app.run_server(debug=True, host='0.0.0.0', port=int(port)) diff --git a/stats/views/__init__.py b/stats/views/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dash/app.py b/stats/views/siret.py similarity index 74% rename from dash/app.py rename to stats/views/siret.py index 403e2ae..ada8222 100644 --- a/dash/app.py +++ b/stats/views/siret.py @@ -1,12 +1,8 @@ -import dash from dash import html, dcc import dash_bootstrap_components as dbc import pandas as pd -import plotly.io as pio -from os import getenv - -app = dash.Dash("trackdechets-public-stats", title='DECP.info : statistiques', external_stylesheets=[dbc.themes.GRID]) -pio.templates.default = "none" +from dash.dependencies import Input, Output +from app import app SIRET = '20004525000012' @@ -30,8 +26,18 @@ def fn(input_number) -> str: return '{:,.0f}'.format(input_number).replace(',', ' ') -def generateYears(years: list) -> [dbc.Row]: - result = [] +layout = html.Div(id='stats') + + +@app.callback(Output('stats', 'children'), + Input('url', 'pathname')) +def generateYears(pathname) -> [dbc.Row]: + siret = pathname.split('/')[2] + print(siret) + years = ['2022', '2021', '2020'] + result = [dbc.Row([ + html.H2('Par année') + ])] for year in years: df_marches_year = df_marches[df_marches['anneeNotification'] == year] df_attributions_year = df_attributions[df_attributions['anneeNotification'] == year] @@ -40,7 +46,7 @@ def generateYears(years: list) -> [dbc.Row]: dbc.Row([ html.H3(year), html.A('Télécharger les données', href=f'https://decp.info/db/decp?_sort=rowid&dateNotification__' - f'startswith={year}&acheteur.id__exact={SIRET}') + f'startswith={year}&acheteur.id__exact={siret}') ]), dbc.Row([ dbc.Col([ @@ -57,20 +63,3 @@ def generateYears(years: list) -> [dbc.Row]: ]) result.append(row) return result - - -acheteur_years = generateYears(['2022', '2021', '2020']) -print(len(acheteur_years)) -app.layout = html.Div(children=[ - dbc.Row([ - html.H2('Par année') - ]) - -] + acheteur_years) - - -if __name__ == '__main__': - port = getenv('PORT', 8050) - - # Scalingo requires 0.0.0.0 as host, instead of the default 127.0.0.1 - app.run_server(debug=bool(getenv('DEVELOPMENT')), host='0.0.0.0', port=int(port))