python-3.x - 如何在破折号框架中沿列表传递数据框?
问题描述
我正在尝试将下拉列表的数据框和选定属性传递给破折号中的不同调用函数。我能够传递选定的值,但我没有得到数据框。这是我的代码。
"""Visualization graph"""
import os
from flask import Flask
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
DIRECTORY_PATH = 'C:/Users/fortu/Desktop/Zuhair/'
directories = [
d for d in (os.path.join(DIRECTORY_PATH, directories) for directories in
os.listdir(DIRECTORY_PATH))
if os.path.isdir(d)
]
external_stylesheets = [
'https://js.api.here.com/v3/3.1/mapsjs-ui.css'
]
# Normally, Dash creates its own Flask server internally. By creating our own,
# we can create a route for downloading files directly:
server = Flask(__name__)
app = dash.Dash(__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div(
[
dbc.Row(dbc.Col(
html.Div("Ground truth visualization",
style={'textAlign': 'center',
'color': 'red', 'fontSize': '40px'}),
md=12)),
dbc.Row(
dbc.Col(
html.Div([
dbc.Row([
dbc.Col(html.Div(dbc.Row(dcc.Dropdown(
id='selected-dir',
options=[{'label': i, 'value': i}
for i in directories],
placeholder="Select the directory"),
style={
'marginLeft': '25px',
'display': 'inline-block',
'width': '90%'
}
)), width=2),
dbc.Col(children=[
html.Div(dbc.Row(dcc.Dropdown(
id='selected-csv-fields',
placeholder="Select the field"),
style={
'display': 'inline-block',
'width': '90%'
}
)),
html.Div(dbc.Row(id='intermediate-value'), style={'display': 'none'})
], width=2),
]),
dbc.Row([
dbc.Col(html.Div([
dbc.Row(
[dbc.Col(html.Div(
dcc.Graph(id='line-graph'),
className='bg-dark badge-dark'))]),
dbc.Row([dbc.Col(html.Div(
dcc.Graph(id='stack-graph'),
className='bg-warning'))],
className="row mt-3")]), width=5)
], className="row mt-3")]))
)
]
)
@app.callback(
[Output("selected-csv-fields", "options"),
Output("intermediate-value", "data")],
[Input("selected-dir", "value")]
)
def get_all_csv(selector):
"""Get all the csv from the folder"""
if selector is not None:
csv_files = list(filter(lambda x: '.csv' in x, os.listdir(selector)))
_DF = pd.concat([pd.read_csv(os.path.join(selector, f))
for f in csv_files])
demo = [{'label': i, 'value': i} for i in _DF]
return demo, _DF
return []
@app.callback(
Output("line-graph", "figure"),
Output("stack-graph", "figure"),
[Input("selected-csv-fields", "value"), Input("intermediate-value", "data")]
)
def update_graph(selector, done):
"""display the graph based on selected value."""
if selector is not None:
fig = px.line(done, x="Time", y=selector)
stack = px.histogram(done, x="Time", y=selector, marginal="box",
hover_data=done)
return fig, stack
return {}, {}
if __name__ == "__main__":
app.run_server(debug=True)
当我从 get_all_csv() 和 Input("intermediate-value", "data") 分别从 update_graph(a, b) 中删除 Output("intermediate-value", "data")] 时,它工作正常。但是当尝试传递这个参数时它给了我错误
ERROR : 更新 selected-csv-fields.options、intermediate-value.data 的回调错误
解决方案
推荐阅读
- spring-boot - Spring Boot 无法将 Redis 客户端 Jedis 连接到池:连接被拒绝
- javascript - 如何在 2D 空间 (x,y) 中制作 3D (X,Y,Z) 形状
- azure - 我可以在 BotBuilder 中使用 PubNub 而不是 DirectLine 吗?
- powershell - Powershell - 命令文本未出现
- python - Python中多维数组的非连续切片
- python - 如何在 scipy 稀疏 lil_matrix 中存储显式 0 值?
- computability - 证明语言 L = {w ∈ {0, 1} ∗ | 输入 x} 的 Mw(x) ↓ 部分可判定但不可判定
- keras - 在具有重建概率的 keras 中实现变分自动编码器
- python - 声明属性后无法从我的列表中删除一行
- html - 带有空格的 Font Awesome 图标的绝对位置是否一致?