我正在将数据上传到仪表板应用程序,并创建了一个放射性项目以选择要从中创建哪种图形。现在,我只创建了一个存储在单独的graph_definitions.py文件中的图形函数:
def lineplot(dat, testmode=False):
'''
testmode: If testomde is set to True only the first 10 items will be used in the graph
'''
print('graph is called')
cells=list(dat['unique_id'].unique())
if testmode==True:
cells=cells[0:10]
#initiating traces as a list
traces=[]
#getting trace IDs from unique IDs (cells)
#looping through the cells
for c in cells:
print('data looping')
#appending x, y data based on current cell to the list of traces
traces.append(go.Scatter(
x=dat.loc[dat['unique_id']==c]['Location_Center_X_Zeroed'],
y=dat.loc[dat['unique_id']==c]['Location_Center_Y_Zeroed']
)
)
print('looping finished')
return {'data' :traces}
有关其余代码,请参见下文。 现在问题如下: 应用程序回调起作用,所有来自graph函数的打印都已创建并且看上去正确,但是该图未更新。 但是,当我忽略if条件使我得到
def get_value(value):
print(value)
print(type(value))
print(GD.lineplot(df, testmode=True))
return GD.lineplot(df, testmode=True)
在单选项目中选择一个值后,它确实可以更新图形。 现在,我不想只拥有一个图形选项,而是想创建多个图形并在它们之间进行选择,因此这没有太大用处。 谁能向我解释为什么条件会造成问题?
import base64
import datetime
import io
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
import sys
import os
sys.path.append(os.path.realpath(__file__))
import graph_definitions as GD
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
#%% app upload
global df
df=[]
app.layout = html.Div([
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Table(id='output-data-upload'),
dcc.RadioItems(
options=[
{'label': 'lineplot', 'value': 'lineplot'},
{'label': 'None', 'value' : 'None'}
],
value='None',
id='graph_selector'),
dcc.Graph(id='migration_data')
])
#%%layouts
def generate_table(df):
return dash_table.DataTable(
data=df.to_dict('records'),
columns=[{'name': i, 'id': i} for i in df.columns],
fixed_rows={'headers':True, 'data':0},
style_cell={'width' :'150px'}
)
#%%
#backup
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
global df
if 'csv' in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
#selection of graphs
return html.Div([
html.H5(filename),
html.H6(datetime.datetime.fromtimestamp(date)),
generate_table(df),
html.Hr(), # horizontal line
# For debugging, display the raw contents provided by the web browser
html.Div('Raw Content'),
html.Pre(contents[0:200] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])
#%% update after upload
@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents')],
[State('upload-data', 'filename'),
State('upload-data', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children
@app.callback(Output('migration_data', 'figure'),
[Input('graph_selector', 'value')])
def get_value(value):
print(value)
if 'df' in globals():
if value=='lineplot':
print(GD.lineplot(df, testmode=True))
return GD.lineplot(df, testmode=True)
编辑: 我可以通过将函数存储在字典中并通过键对其进行调用来解决此问题。
graph_options={'lineplot':GD.lineplot}
def get_value(value):
return graph_options[value](df, testmode=True)
在无法使用条件的情况下,但这仅在所有图的输入相同时才起作用。