Hi,
- I parse uploaded data and return df_con:
Result: OK
def parse_data(contents, filename):
content_type, content_string = contents.split(',')
global df_con
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV or TXT file
df_con = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df_con = pd.read_excel(io.BytesIO(decoded))
elif 'txt' or 'tsv' in filename:
# Assume that the user upl, delimiter = r'\s+'oaded an excel file
df_con = pd.read_csv(
io.StringIO(decoded.decode('utf-8')), delimiter = r'\s+')
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
return df_con
- Then create callback and make a Scatter.
Result: Chart is OK, but there is (minor) error “local variable ‘df_con’ referenced before assignment”.
@app.callback(Output('graph3', 'figure'),
[Input('upload-data', 'contents'),
Input('upload-data', 'filename')])
def update_graph_upload(contents, filename):
if contents:
contents = contents[0]
filename = filename[0]
df_con = parse_data(contents, filename)
fig2 = go.Figure(
data=[
go.Scatter(
x=df_con['datetime'],
y=df_con['fact'],
mode='lines+markers')
],
layout=go.Layout(
plot_bgcolor=colors["graphBackground"],
paper_bgcolor=colors["graphBackground"]
))
return fig2
- Create a function to analize uploaded data and return dfcnb
Result: Seems OK. (working on others script)
def bal_costs_df(df_con):
con = pyodbc.connect(
'Driver={SQL Server};'
'Server=HTIC--DB1.RF.RES;'
'Database=EKX;'
'Trusted_Connection=yes;'
)
df_con['plan'] = df_con['fact'].shift(168)
querystringnp = f"""
SELECT [ELSPOTP_LT_DATE_ID],
[ELSPOTP_LT_TIME_ID],
[ELSPOTP_PRICE]
FROM [ET_DWH].[dbo].[FactElspotPrices]
WHERE ELSPOTP_PAREA_ID = 1
ORDER BY ELSPOTP_LT_DATE_ID
"""
cursor = con.cursor()
df_np = pd.read_sql(querystringnp, con)
#Many other data manipulation and return....
return dfcnb
- Try to create another chart based on df_con and bal_costs_df function.
Result: Got error name ‘df_con’ is not defined.
@app.callback(Output('graph4', 'figure'),
[Input('upload-data', 'contents'),
Input('upload-data', 'filename')])
def update_graph2(contents, filename):
dfm = bal_costs_df(df_con) <<<<<<<<<<<<ERROR: "name 'df_con' is not defined"
fig3 = go.Figure(
data=[
go.Scatter(
x=dfm['datetime'],
y=dfm['balcosts'],
mode='lines+markers')
],
layout=go.Layout(
plot_bgcolor=colors["graphBackground"],
paper_bgcolor=colors["graphBackground"]
))
return fig3
Any ideas why for graph3 is ok, but for graph4 df_con is not defined? Maybe inputs are wrong?
I want to have data as in dfcnb dataframe and create a chart from it.
Thanks