折线图未在多页绘图破折号上显示数据
Line chart not showing data on multi-page plotly dash
这里出现奇怪的错误。单页虚线在我的折线图上显示数据,但是当我创建多页虚线时,折线图不再显示数据。
只是显示这个。
虽然我的代码是一样的,但似乎找不到显示折线图数据的方法。
index.js
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
nav_item = dbc.NavItem(dbc.NavLink("Home", href="https://www.google.com"))
# make a reuseable dropdown for the different examples
dropdown = dbc.DropdownMenu(
children=[
dbc.DropdownMenuItem("Home", href='http://1577.6.0.1:9999/apps/main'),
dbc.DropdownMenuItem(divider=True),
dbc.DropdownMenuItem("Login / Sign-Up", href='http://127.0.0.1:8050/apps/test')
],
nav=True,
in_navbar=True,
label="Important Links",
)
navbar = dbc.Navbar(
dbc.Container(
[
html.A(
# Use row and col to control vertical alignment of logo / brand
dbc.Row(
[
dbc.Col(dbc.NavbarBrand("something", className="ml-2",)),
],
align="center",
no_gutters=True,
),
href="https://plot.ly",
),
dbc.NavbarToggler(id="navbar-toggler2"),
dbc.Collapse(
dbc.Nav(
[
nav_item,
dropdown,
], className="ml-auto", navbar=True
),
id="navbar-collapse2",
navbar=True,
),
]
),
color="#ED4651",
dark=True,
className="mb-5",
)
app.layout = html.Div([
navbar,
dcc.Location(id='url', refresh=False),
html.H4("Some Title"),
html.Div(id='page-content')
])
@app.callback(Output('page-content', 'children'),
Input('url', 'pathname'))
def display_page(pathname):
if pathname == '/apps/login':
return login.layout
elif pathname == '/apps/main':
return main.layout
else:
return '404'
if __name__ == '__main__':
app.run_server(debug=True)
main.py
state = pd.read_csv("/U********************.csv", index_col=None)
m1 = 0
m2 = 0
m3 = 0
unique = state["Area"].unique()
sq = ["< 500", "500 to 1000", "1000+", "All Sizes"]
sqVal = []
external_stylesheets = ['https://codepen.io/chrisudoddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
opts = []
for i in unique:
opts.append({'label': i, 'value': i})
layout = html.Div(children=[
html.H1(children='stats'),
html.Div(children='''
Simple stuff.
'''),
dcc.Dropdown(
id="my-drop",
options=opts,
multi=True,
value=[unique[0]]
),
#html.Button('Update Graph', id='submit-val', n_clicks=0)
dcc.Graph(id='example-graph')
])
@app.callback(
Output('example-graph', 'figure'),
Input('my-drop', 'value'),
)
def update_output_div(input_value):
check = []
figures = []
unique = input_value
for i in unique:
m1 = 0
m2 = 0
m3 = 0
for index, row in state.iterrows():
if (row["Area"] == i):
if row["Property Type"] == "Commerical":
m1 += row["Price"]
if row["Property Type"] == "Residential":
m2 += row["Price"]
if row["Property Type"] == "Res-Rental":
m3 += row["Price"]
check.extend([m1, m2, m3])
frames = []
data = {'Property Type': state["Property Type"].unique()}
frames.append(pd.DataFrame(data))
for a in unique:
newset = []
for s in range(3):
newset.append(check[s])
complete = {a: newset}
frames.append(pd.DataFrame(complete))
check = check[3:]
result = pd.concat(frames, axis=1)
fig = go.Figure()
# fig = px.line(result, x=result['Property Type'], y=result[unique[0]], title="Analysis of Price over Property Type")
# unique = unique[1:]
for k in unique:
fig.add_trace(go.Scatter(x=result['Property Type'], y=result[k], name=k,
line_shape='linear'))
# fig.add_scatter(x=result['Property Type'], y=result[k], name=k)
fig.update_layout(title="Price by Property Type",
xaxis_title="Property Type",
yaxis_title="Price",
legend_title="Areas")
return fig
if __name__ == '__main__':
app.run_server(debug=True)
dash 的新手所以任何帮助都会很棒。谢谢!
我无法确认,因为你没有分享你的进口,但似乎 index
进口 main
和 main
进口 index
.
您的设置方式导致 app
无法使用您在 main.py
中定义的回调函数进行修饰,从而导致图表为空。
有多种方法可以做到这一点,但一个想法是创建一个函数,将 app
作为参数并用回调函数装饰它:
# inside main.py
def init_callbacks(app):
@app.callback(
Output("example-graph", "figure"),
Input("my-drop", "value"),
)
def update_output_div(input_value):
# Do stuff...
然后在 index.py
里面你可以做这样的事情:
import main
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
main.init_callbacks(app)
这样 app
由 update_output_div
回调修饰。
请记住,当执行 main.init_callbacks(app)
时,main
模块 layout
中的元素还不存在于 app
的初始 layout
中.所以它可能会给你一个关于布局中不存在的某些 id 的元素的警告。为防止这种情况,您可以将具有这些 ID 作为子代的占位符元素添加到 index.py
内的 page-content
,或者您可以将 suppress_callback_exceptions
设置为 True
。
这里出现奇怪的错误。单页虚线在我的折线图上显示数据,但是当我创建多页虚线时,折线图不再显示数据。
只是显示这个。
虽然我的代码是一样的,但似乎找不到显示折线图数据的方法。
index.js
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
nav_item = dbc.NavItem(dbc.NavLink("Home", href="https://www.google.com"))
# make a reuseable dropdown for the different examples
dropdown = dbc.DropdownMenu(
children=[
dbc.DropdownMenuItem("Home", href='http://1577.6.0.1:9999/apps/main'),
dbc.DropdownMenuItem(divider=True),
dbc.DropdownMenuItem("Login / Sign-Up", href='http://127.0.0.1:8050/apps/test')
],
nav=True,
in_navbar=True,
label="Important Links",
)
navbar = dbc.Navbar(
dbc.Container(
[
html.A(
# Use row and col to control vertical alignment of logo / brand
dbc.Row(
[
dbc.Col(dbc.NavbarBrand("something", className="ml-2",)),
],
align="center",
no_gutters=True,
),
href="https://plot.ly",
),
dbc.NavbarToggler(id="navbar-toggler2"),
dbc.Collapse(
dbc.Nav(
[
nav_item,
dropdown,
], className="ml-auto", navbar=True
),
id="navbar-collapse2",
navbar=True,
),
]
),
color="#ED4651",
dark=True,
className="mb-5",
)
app.layout = html.Div([
navbar,
dcc.Location(id='url', refresh=False),
html.H4("Some Title"),
html.Div(id='page-content')
])
@app.callback(Output('page-content', 'children'),
Input('url', 'pathname'))
def display_page(pathname):
if pathname == '/apps/login':
return login.layout
elif pathname == '/apps/main':
return main.layout
else:
return '404'
if __name__ == '__main__':
app.run_server(debug=True)
main.py
state = pd.read_csv("/U********************.csv", index_col=None)
m1 = 0
m2 = 0
m3 = 0
unique = state["Area"].unique()
sq = ["< 500", "500 to 1000", "1000+", "All Sizes"]
sqVal = []
external_stylesheets = ['https://codepen.io/chrisudoddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
opts = []
for i in unique:
opts.append({'label': i, 'value': i})
layout = html.Div(children=[
html.H1(children='stats'),
html.Div(children='''
Simple stuff.
'''),
dcc.Dropdown(
id="my-drop",
options=opts,
multi=True,
value=[unique[0]]
),
#html.Button('Update Graph', id='submit-val', n_clicks=0)
dcc.Graph(id='example-graph')
])
@app.callback(
Output('example-graph', 'figure'),
Input('my-drop', 'value'),
)
def update_output_div(input_value):
check = []
figures = []
unique = input_value
for i in unique:
m1 = 0
m2 = 0
m3 = 0
for index, row in state.iterrows():
if (row["Area"] == i):
if row["Property Type"] == "Commerical":
m1 += row["Price"]
if row["Property Type"] == "Residential":
m2 += row["Price"]
if row["Property Type"] == "Res-Rental":
m3 += row["Price"]
check.extend([m1, m2, m3])
frames = []
data = {'Property Type': state["Property Type"].unique()}
frames.append(pd.DataFrame(data))
for a in unique:
newset = []
for s in range(3):
newset.append(check[s])
complete = {a: newset}
frames.append(pd.DataFrame(complete))
check = check[3:]
result = pd.concat(frames, axis=1)
fig = go.Figure()
# fig = px.line(result, x=result['Property Type'], y=result[unique[0]], title="Analysis of Price over Property Type")
# unique = unique[1:]
for k in unique:
fig.add_trace(go.Scatter(x=result['Property Type'], y=result[k], name=k,
line_shape='linear'))
# fig.add_scatter(x=result['Property Type'], y=result[k], name=k)
fig.update_layout(title="Price by Property Type",
xaxis_title="Property Type",
yaxis_title="Price",
legend_title="Areas")
return fig
if __name__ == '__main__':
app.run_server(debug=True)
dash 的新手所以任何帮助都会很棒。谢谢!
我无法确认,因为你没有分享你的进口,但似乎 index
进口 main
和 main
进口 index
.
您的设置方式导致 app
无法使用您在 main.py
中定义的回调函数进行修饰,从而导致图表为空。
有多种方法可以做到这一点,但一个想法是创建一个函数,将 app
作为参数并用回调函数装饰它:
# inside main.py
def init_callbacks(app):
@app.callback(
Output("example-graph", "figure"),
Input("my-drop", "value"),
)
def update_output_div(input_value):
# Do stuff...
然后在 index.py
里面你可以做这样的事情:
import main
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
main.init_callbacks(app)
这样 app
由 update_output_div
回调修饰。
请记住,当执行 main.init_callbacks(app)
时,main
模块 layout
中的元素还不存在于 app
的初始 layout
中.所以它可能会给你一个关于布局中不存在的某些 id 的元素的警告。为防止这种情况,您可以将具有这些 ID 作为子代的占位符元素添加到 index.py
内的 page-content
,或者您可以将 suppress_callback_exceptions
设置为 True
。