Working data gathering, showing data

This commit is contained in:
Colin Maudry
2022-03-20 21:05:45 +01:00
parent b67bf183f8
commit befbf2914a
2 changed files with 64 additions and 32 deletions
+7 -5
View File
@@ -9,22 +9,24 @@ pio.templates.default = "none"
app.layout = html.Div([ app.layout = html.Div([
dcc.Store(id='memory'),
dcc.Location(id='url', refresh=False), dcc.Location(id='url', refresh=False),
html.Div(id='page-content') html.Div(id='page-content'),
html.Div(id='debug')
]) ])
@app.callback(Output('page-content', 'children'), @app.callback(Output('page-content', 'children'),
# Output('debug', 'children'),
Input('url', 'pathname')) Input('url', 'pathname'))
def display_page(pathname): def display_page(pathname):
if pathname.startswith('/siret/'): if pathname.startswith('/siret/'):
return siret.layout return [siret.layout]
else: else:
return '404' return ['Page inconnue...', pathname]
if __name__ == '__main__': if __name__ == '__main__':
port = getenv('PORT', 8050) 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)) app.run_server(debug=True, host='0.0.0.0', port=int(port))
+54 -24
View File
@@ -1,52 +1,82 @@
import json
from dash import html, dcc from dash import html, dcc
import dash_bootstrap_components as dbc import dash_bootstrap_components as dbc
import pandas as pd import pandas as pd
from dash.dependencies import Input, Output from dash.dependencies import Input, Output
from app import app from app import app
import re
SIRET = '20004525000012' SIRET = '20004525000012'
df_marches: pd.DataFrame = pd.read_csv(
'https://decp.info/db/decp-sans-titulaires.csv?_sort=uid&acheteur.id__exact=20004525000012&_size=max&_dl=1',
true_values=['oui'],
false_values=['non'])
# df_marches = df_marches[df_marches['donneesActuelles'] == 'oui']
df_marches['anneeNotification'] = df_marches['dateNotification'].str[:4]
df_attributions: pd.DataFrame = pd.read_csv(
f'https://decp.info/db/decp.csv?_sort=uid&acheteur.id__exact={SIRET}&_size=max&_dl=1',
true_values=['oui'],
false_values=['non'])
# df_attributions = df_attributions[df_attributions['donneesActuelles'] == 'oui']
df_attributions['anneeNotification'] = df_attributions['dateNotification'].str[:4]
def fn(input_number) -> str: def fn(input_number) -> str:
return '{:,.0f}'.format(input_number).replace(',', ' ') return '{:,.0f}'.format(input_number).replace(',', ' ')
def get_siret(pathname: str) -> str:
return pathname.split('/')[2]
layout = html.Div(id='stats') layout = html.Div(id='stats')
@app.callback(Output('memory', 'data'),
Input('url', 'pathname'),
Input('memory', 'data'))
def get_marches(pathname, data):
regex = re.compile('^/siret/[0-9a-z]{14}$')
if regex.match(pathname):
siret = get_siret(pathname)
if type(data) == dict and data['siret']:
pass
df_marches: pd.DataFrame = pd.read_csv(
f'https://decp.info/db/decp-sans-titulaires.csv?_sort=uid&acheteur.id__exact={siret}&_size=max&_dl=1',
true_values=['oui'],
false_values=['non'])
# df_marches = df_marches[df_marches['donneesActuelles'] == 'oui']
df_marches['anneeNotification'] = df_marches['dateNotification'].str[:4]
df_attributions: pd.DataFrame = pd.read_csv(
f'https://decp.info/db/decp.csv?_sort=uid&acheteur.id__exact={siret}&_size=max&_dl=1',
true_values=['oui'],
false_values=['non'])
# df_attributions = df_attributions[df_attributions['donneesActuelles'] == 'oui']
df_attributions['anneeNotification'] = df_attributions['dateNotification'].str[:4]
datasets = {
'marches': df_marches.to_json(date_format='iso', orient='split'),
'attributions': df_attributions.to_json(date_format='iso', orient='split'),
}
return json.dumps(datasets)
@app.callback(Output('stats', 'children'), @app.callback(Output('stats', 'children'),
Input('url', 'pathname')) Input('url', 'pathname'),
def generateYears(pathname) -> [dbc.Row]: Input('memory', 'data'))
def generateYears(pathname, json_data) -> [dbc.Row]:
siret = pathname.split('/')[2] siret = pathname.split('/')[2]
print(siret) print(siret)
years = ['2022', '2021', '2020'] years = ['2022', '2021', '2020']
result = [dbc.Row([ result = [dbc.Row([
html.H2('Par année') html.H2('Par année')
])] ])]
dataset = json.loads(json_data)
df_marches = pd.read_json(dataset['marches'], orient='split')
for year in years: for year in years:
df_marches_year = df_marches[df_marches['anneeNotification'] == year] df_marches_year = df_marches[df_marches['anneeNotification'] == int(year)]
df_attributions_year = df_attributions[df_attributions['anneeNotification'] == year] # df_attributions_year = df_attributions[df_attributions['anneeNotification'] == year]
row = dbc.Row([ row = dbc.Row([
dbc.Col([ dbc.Col([
dbc.Row([ dbc.Row([
html.H3(year), html.H3(year),
html.A('Télécharger les données', href=f'https://decp.info/db/decp?_sort=rowid&dateNotification__' html.A('Télécharger les données', href=f'https://decp.info/db/decp.xlsx?'
f'startswith={year}&acheteur.id__exact={siret}') f'_sort=rowid'
f'&dateNotification__startswith={year}'
f'&acheteur.id__exact={siret}'
f'&_size=max&_dl=1')
]), ]),
dbc.Row([ dbc.Row([
dbc.Col([ dbc.Col([
@@ -55,9 +85,9 @@ def generateYears(pathname) -> [dbc.Row]:
dbc.Col([ dbc.Col([
html.H4('Montant total attribué'), html.H4('Montant total attribué'),
html.P(fn(df_marches_year['montant'].sum()) + ' euros')]), html.P(fn(df_marches_year['montant'].sum()) + ' euros')]),
dbc.Col([ # dbc.Col([
html.H4('Titulaires différents'), # html.H4('Titulaires différents'),
html.P(str(df_attributions_year['titulaire.id'].unique().size))]) # html.P(str(df_attributions_year['titulaire.id'].unique().size))])
]) ])
]) ])
]) ])