Hey @vantaka2,
Thanks for the quick response!
Here’s my file structure:
| index.py
| app.py
| data.csv
|
| apps
|----__init __.py
|---- comparison.py
|---- dashboard.py
|
| all_components
|----__init __.py
|----navbar.py
I’ve tweaked the code a bit. After a few fixes, I’ve managed to make the graphs work - however, they only work after first selecting a dropdown menu item first. The initial loading of the screen still throws the same error from above when reading the data from the JSON.
I’ve attached the code below. I’ve simplified it a lot so that it’s easier to digest. Let me know what you think.
Thanks a lot!
index.py
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
from apps import dashboard, comparison
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 == '/apps/dashboard':
return dashboard.layout
elif pathname == '/apps/comparison':
return comparison.layout
else:
return "error 404"
if __name__ == '__main__':
app.run_server(debug=True)
app.py
import dash
app = dash.Dash(__name__)
server = app.server
app.config['suppress_callback_exceptions']=True
navbar.py
import dash_bootstrap_components as dbc
navbar = dbc.NavbarSimple(
children=[
dbc.NavItem(dbc.NavLink("Overview",href = "/apps/dashboard")),
dbc.DropdownMenu(
nav=True,
in_navbar=True,
label="Analytical Tools",
children=[
dbc.DropdownMenuItem(dbc.NavLink("Student Performance Tracker", href="/apps/comparison")),
dbc.DropdownMenuItem(dbc.NavLink("Analytical Tool 2", href="#")),
dbc.DropdownMenuItem(dbc.NavLink("Analytical Tool 3",href="#")),
],
),
dbc.NavItem(dbc.NavLink("Feedback",href = "/apps/feedback")),
dbc.NavItem(dbc.NavLink("Support",href = "#"))
],
brand="Dashboard",
brand_href="#",
sticky="top",
dark = True,
color = 'primary'
)
dashboard.py
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import dash
import pandas as pd
import plotly.graph_objs as go
import numpy as np
from all_components.navbar import navbar
from app import app
""""""""""""""""""""""""""""""
File Extraction and Editing
""""""""""""""""""""""""""""""
df = pd.read_csv('data.csv', sep='\t', encoding='utf-8')
data manipulation stuff
print(df_marks.head())
print(type(df_marks))
"""""""""""""""""""""
Plot Bar Functions
"""""""""""""""""""""
def plot_bar(frame,type,chart,title):
code that plots
return {data, layout}
"""""""""""""""""""""
Body of the Layout
"""""""""""""""""""""
body = dbc.Container(
[
dbc.Row(
[
dbc.Col(
[
dcc.Dropdown(
id = "gender_dropdown",
options = [
{'label': 'Male','value': 'M'},
{'label': 'Female', 'value':'F'}
],
placeholder = "Gender",
style = dict(
lineHeight = '12px'
)
),
]
),
dbc.Col(
[
dcc.Graph(id = 'marks_graph')
]
)
], className = "mt-4"
),
html.Div(id = "hidden", style = dict(display='None'),children = [])
]
)
"""""""""
Layout
"""""""""
layout = html.Div([navbar, body])
""""""""""""
Callbacks
""""""""""""
@app.callback(dash.dependencies.Output('hidden', 'children'), [dash.dependencies.Input('gender_dropdown', 'value')])
def clean_data(value):
if value is None:
cleaned_df = df_marks
else:
cleaned_df = df_marks[df_marks['gender'] == value]
return cleaned_df.to_json(date_format='iso', orient='split')
@app.callback(
dash.dependencies.Output(component_id='marks_graph', component_property='figure'),
[dash.dependencies.Input(component_id='hidden', component_property='children')]
)
def update_marks_graph(jsonified_cleaned_data):
dff = pd.read_json(jsonified_cleaned_data, orient='split')
return plot_bar(dff,"Marks","bar","Average Score per Test")
comparison.py
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from app import app
layout = html.Div([
html.H3('App 1'),
dbc.NavItem(dbc.NavLink("Link", href="/apps/dashboard")),
dcc.Dropdown(
id='app-2-dropdown',
options=[
{'label': 'App 1 - {}'.format(i), 'value': i} for i in [
'NYC', 'MTL', 'LA'
]
]
),
html.Div(id='app-2-display-value'),
dcc.Link('Go to App 1', href='/apps/dashboard')
])
@app.callback(
Output('app-2-display-value', 'children'),
[Input('app-2-dropdown', 'value')])
def display_value(value):
return 'You have selected "{}"'.format(value)