hi im facing issue in displaying graph. when I use @app.callback(Output(‘Mygraph’, ‘figure’) decorator my graph is not displaying but if i comment that my graph is working
import base64
import datetime
import io
import plotly.graph_objs as go
import cufflinks as cf
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 plotly.express as px
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__,external_stylesheets=external_stylesheets)
colors = {
"graphBackground": "#F5F5F5",
"background": "#ffffff",
"text": "#000000"
}
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'},
multiple=True),
html.Div([dcc.Tabs(id="tabs", value= None,
children=[dcc.Tab(label='Bar', value='bar',
children= [html.Div(id='bar-content')]),
dcc.Tab(label='Pie', value='pie',
children= [html.Div(id='pie-content')]
)
]
),
html.Div(id='tabs-content')
]),
dcc.Graph(id='Mygraph'),
html.Div(id='output-data-upload')
])
def parse_data(contents, filename):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV or TXT file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
return df
@app.callback(Output('bar-content', 'children'),
Input('upload-data', 'contents'),
Input('upload-data', 'filename'))
def bar_content(contents, filename):
bar_buttons = html.Div()
if contents:
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
columns = df.columns
bar_buttons = html.Div([html.Label('X'),dcc.Dropdown(id = 'bar-X-axis',options =[{"label": name, "value": name} for name in columns],
value=None),
html.Label('Y'),dcc.Dropdown(id = 'bar-Y-axis',options =[{"label": name, "value": name} for name in columns],
value=None,multi=True),
html.Label('Enter Title for Graph'),dcc.Input(id ='bar-title',value ='Enter Title',type ='text'),
html.Button('submit', id='bar_submit', n_clicks=0)])
return bar_buttons
@app.callback(Output('pie-content', 'children'),
Input('upload-data', 'contents'),
Input('upload-data', 'filename'))
def pie_content(contents, filename):
pie_buttons = html.Div()
if contents:
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
columns = df.columns
pie_buttons = html.Div([html.Label('Values'),dcc.Dropdown(id = 'pie_values',options =[{"label": name, "value": name} for name in columns],
value=None),
html.Label('Names'),dcc.Dropdown(id = 'pie_names',options =[{"label": name, "value": name} for name in columns],
value=None),
html.Label('Enter Title for Graph'),dcc.Input(id ='pie-title',value ='Enter Title',type ='text'),
html.Button('submit', id='pie_submit', n_clicks=0)])
return pie_buttons
@app.callback(Output('Mygraph', 'figure'),
[
Input('upload-data', 'contents'),
Input('upload-data', 'filename'),
])
def update_graph(contents, filename):
if contents:
contents = contents[0]
filename = filename[0]
( here i will write my code to return graph)
return fig
@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents'),
Input('upload-data', 'filename')])
def update_table(contents, filename):
table = html.Div()
if contents:
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
table = html.Div([
html.H5(filename),
dash_table.DataTable(
data=df.to_dict('rows'),
columns=[{'name': i, 'id': i} for i in df.columns]
),
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents[0:200] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])
return table
app.run_server()