Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about the 'Side Navigation Bar' #15

Closed
q1121599315 opened this issue Oct 19, 2021 · 1 comment
Closed

Question about the 'Side Navigation Bar' #15

q1121599315 opened this issue Oct 19, 2021 · 1 comment

Comments

@q1121599315
Copy link

Hey Adam!
I looked at the video of "Side Navigation Bar" on your youtube channel,and I got a question, I want to design a complicated app by just using side bar. In order to do this , I have to create another app, but I don't known how to deal with these two callbacks.
the code as following:

import dash
import dash_bootstrap_components as dbc
from dash import html,dcc
import plotly.express as px
from dash.dependencies import Input,Output
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/Coding-with-Adam/Dash-by-Plotly/master/Bootstrap/Side-Bar/iranian_students.csv')

app2=dash.Dash(name,external_stylesheets=[dbc.themes.BOOTSTRAP],suppress_callback_exceptions=True)
server = app2.server
df2 = px.data.tips()
days = df2.day.unique()
app2.layout = html.Div([
dcc.Dropdown(
id="dropdown",
options=[{"label": x, "value": x} for x in days],
value=days[0],
clearable=False,
),
dcc.Graph(id="bar-chart"),
])

@app2.callback(
Output("bar-chart", "figure"),
[Input("dropdown", "value")])
def update_bar_chart(day):
mask = df2["day"] == day
fig = px.bar(df2[mask], x="sex", y="total_bill",
color="smoker", barmode="group")
return fig

app=dash.Dash(name,external_stylesheets=[dbc.themes.BOOTSTRAP])

SIDEBAR_STYLE={
'position':'fixed',
'top':0,
'left':0,
'bottom':0,
'width':'16rem',
'padding':'2rem 1rem',
'background-color':'#f8f9fa',
}

CONTENT_STYLE={
'margin-left':'18rem',
'margin-right':'2rem',
'padding':'2rem 1rem',
}

sidebar=html.Div(
[
html.H2('Sidebar',className='display-4'),
html.Hr(),
html.P(
'Number of students per education level',className='lead'
),
dbc.Nav(
[
dbc.NavLink('Home',href='/',active='exact'),
dbc.NavLink('Page 1',href='/page-1',active='exact'),
dbc.NavLink('Page 2',href='/page-2',active='exact'),
],
vertical=True,
pills=True,#
),
],
style=SIDEBAR_STYLE,
)

content=html.Div(id='page-content',children=[],style=CONTENT_STYLE)

app.layout=html.Div([
dcc.Location(id='url'),
sidebar,
content
])
@app.callback(
Output('page-content','children'),
[Input('url','pathname')]
)
def render_page_content(pathname):
if pathname=='/':
return [app2.layout]
elif pathname =='/page-1':
return [
html.H1('Grad School in Iran',
style={'textAlign':'center'}),
dcc.Graph(id='bargraph',
figure=px.bar(df,barmode='group',x='Years',
y=['Girls Grade School','Boys Grade School']))
]
elif pathname =='/page-2':
return [
html.H1('High School in Iran',
style={'textAlign':'center'}),
dcc.Graph(id='bargraph',
figure=px.bar(df,barmode='group',x='Years',
y=['Girls High School','Boys High School']))
]
return dbc.Jumbotron(
[
html.H1('404 : Not found' ,className='text-danger'),
html.Hr(),
html.P(f'The pathname {pathname} was not recognised...')
]
)
if name=='main':
app.run_server(debug=True,port=3001)

I have also looked at the video of Deploy_App_to_Web\Multipage_App on your channel , but i think that using side bar is much more easier than building a multipage app. Looking for you support.

@Coding-with-Adam
Copy link
Owner

Coding-with-Adam commented Oct 19, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants