I am having trouble to refresh the dataframe I extract from a mongDB
When I reload a page and print the dataframe it is indeeed reloaded but it does not appear modified in the page.
I suspect a problem with the dropdownrefreshing …
Here is the code
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import dash_auth
import pandas as pd
import pymongo
import datetime
app = dash.Dash()
db = Get_local_Mongo(‘CompassFT’)
app.css.append_css({
“external_url”: “https://codepen.io/chriddyp/pen/bWLwgP.css”
})
def get_data_object(user_selection):
return dataframes[user_selection]
def generate_table(dataframe, max_rows=100):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
)
dataframes = load_all_DB()
def serve_layout():
dataframes = load_all_DB()
layout = html.Div([
html.H3(‘Compass-ft Mongo Data Base’),
html.H4('Today is the : ’ + str(datetime.datetime.now())),
html.Label(‘Choose table / collection:’, style={‘font-weight’: ‘bold’}),
dcc.Dropdown(
id=‘field-dropdown’,
options=[{‘label’: df, ‘value’: df} for df in dataframes],
value=‘ActiveContractList’,
clearable=False
),
dt.DataTable(
# Initialise the rows
rows=[{}],
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=[],
id=‘table’
),
html.Div(id=‘selected-indexes’),
#html.Div(id=‘table2’),
html.A(
‘Download Data’,
id=‘download-link’,
download=‘Data.csv’,
href="",
target="_blank"
)
], className=‘container’)
return layout
app.layout = serve_layout
@app.callback(Output(‘table’, ‘rows’), [Input(‘field-dropdown’, ‘value’)])
def update_table(user_selection):
df = get_data_object(user_selection)
return df.to_dict(‘records’)
@app.callback(dash.dependencies.Output(‘download-link’, ‘href’), [Input(‘field-dropdown’, ‘value’)])
def update_download_link(user_selection):
dff = dataframes[user_selection]
csv_string = dff.to_csv(index=False, encoding=‘utf-8’)
csv_string = “data:text/csv;charset=utf-8,%EF%BB%BF” + quote(csv_string)
return csv_string
if name == ‘main’:
app.run_server(debug=True, port = 8050)