Hi Guys,
I have been trying to create a DataTable with a varying number of rows, by choosing number of rows via a dropdown component. For this I used the following code:
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import json
import pandas as pd
import numpy as np
import plotly
import plotly.figure_factory as ff
import plotly.graph_objs as go
app = dash.Dash()
app.scripts.config.serve_locally = True
app.css.config.serve_locally = False
DF_MCA = pd.read_csv('D:\Box Sync\My Box (907070)\Energietransitie pres\MCA_Basis.csv')
DF_MCA.loc[0:20]
DF_MCA.rename(columns={'Unnamed: 0': 'Naam Idee'}, inplace=True)
ROWS = [
{'a': 'AA', 'b': 1},
{'a': 'AB', 'b': 2},
{'a': 'BB', 'b': 3},
{'a': 'BC', 'b': 4},
{'a': 'CC', 'b': 5},
{'a': 'CD', 'b': 6}
]
app.layout = html.Div([
# Page Header
html.Div([
html.Div([
html.H1(
children='MCA Tool',
style={
'fontSize':60,
'textAlign':'center',
'textDirection': 'vertical',
'dir':'rtl'
},
className='eight columns')
]),
html.Div([
html.Img(
src = 'http://denatuurlijkestad.nl/wp-content/uploads/2016/12/RHDHV-logo-download_0.jpg',
className='three columns')
]),
]),
##Dropdown menu choosing number of ideas
html.Div([
dcc.Dropdown(
id = 'NrIdeas',
options=[
{'label':'1','value':1},
{'label':'2','value':2},
{'label':'3','value':3},
{'label':'4','value':4},
{'label':'5','value':5},
{'label':'6','value':6},
{'label':'7','value':7},
{'label':'8','value':8},
{'label':'9','value':9},
{'label':'10','value':10}
],
value=4
)
]),
# Match Results Grid
html.Div([
# Match Results Table
html.Div(
id='MCA-Datatable',
className='ten columns',
),
# Season Summary Table and Graph
html.Div([
# summary table
html.Div(id='MCA-Totaal'),
], className="three columns")
], className="row"),
html.Div([
# graph
html.Div(id='selected-indexes'),
dcc.Graph(
id='graph-MCA'
),
], className="ten columns"),
html.Div(id='TussenTabel'),
html.Div(id='TussenDF'),
html.Div(
dt.DataTable(
id='ONZ',
rows=[{}]),
style={'display':'none'},
),
])
def make2dList(rows, cols):
a=[]
for row in range(rows): a += [[0]*cols]
return a
@app.callback(Output('MCA-Datatable','children'),[Input('NrIdeas','value')])
def Resize_DF(value):
TussenLijst1 = make2dList(value,8)
DF_MCA_Var = pd.DataFrame(TussenLijst1)
DF_MCA_Var.rename(columns={0: 'Naam Idee'}, inplace=True)
DF_MCA_Var.rename(columns={1: 'Duurzaamheid (CO2-winst)'}, inplace=True)
DF_MCA_Var.rename(columns={2: 'Betrouwbaarheid spoor (technisch)'}, inplace=True)
DF_MCA_Var.rename(columns={3: 'Onderhoudbaarheid spoor'}, inplace=True)
DF_MCA_Var.rename(columns={4: 'Veilig spoor (Intern & extern)'}, inplace=True)
DF_MCA_Var.rename(columns={5: 'Imago ProRail'}, inplace=True)
DF_MCA_Var.rename(columns={6: 'Maatschappelijke baat bij gebruik huidige infrastructuur'}, inplace=True)
DF_MCA_Var.rename(columns={7: 'Politieke component met ministerie EZ & IM'}, inplace=True)
RowsCount = len(DF_MCA_Var.index)
for i in range(0,RowsCount):
DF_MCA_Var['Naam Idee'][i]='idee '+ str(i+1)
return dt.DataTable(
rows=DF_MCA_Var.to_dict('records'),
)
Because I created an empty DataTable object in the app.layout (as suggested in earlier posts, thanks!) This callback produces a DataTable with a varying number of rows. I would like to use the output of this table for further calculation. Because the DataTable is outputted to âchildrenâ of an empty html.Div, the data can only be accessed via this âchildrenâ property, instead of the much more beautiful prop ârowsâ.
It seems I am only able to access the property ârowsâ when I output the DataTable in the callback function, to a defined DataTable in app.layout. That however, results in the âError loading dependanciesâ.
The âchildrenâ prop contains a JSON formatted version of the previous called DataTable. Python seems to think this is a dictionary, but found functions like json.read etc. canât read the âdictâ. It looks like this (with print(children) in cmd):
{'props': {'rows': [{'Naam Idee': 'idee 1', 'Duurzaamheid (CO2-winst)': 0, 'Betrouwbaarheid spoor (technisch)': 0, Onderhoudbaarheid spoor': 0, 'Veilig spoor (Intern & extern)': 0, 'Imago ProRail': 0, 'Maatschappelijke baat bij gebruik huidige infrastructuur': 0, 'Politieke component met ministerie EZ & IM': 0}, {'Naam Idee': 'idee 2', 'Duurzaamheid (CO2-winst)': 0, 'Betrouwbaarheid spoor (technisch)': 0, 'Onderhoudbaarheid spoor': 0, 'Veilig spoor (Intern & extern)': 0, 'Imago ProRail': 0, 'Maatschappelijke baat bij gebruik huidige infrastructuur': 0,'Politieke component met ministerie EZ & IM': 0}]},'type': 'DataTable', 'namespace': 'dash_table_experiments'}
Could someone shed some light on the âError Loading Dependanciesâ error? Or can someone help figure out a way to read the children prop properly?
Kind regards,
Stuart