Hi,
I am experimenting with visualising data in Dash. The basics went well but now, I am stuck on following issue:
In my callback, I would like to access a function I have written (and is functioning well in other scripts).
This function (calculate_rel_diff) is written in “utilities” which I imported.
Although I did the import, it seems that I cannot access this function?
Is it correct that from a callback, I cannot access other functions? How can I solve this?
I do following:
read data fro1. m csv to pandas dataframe
2. predefine pandas series to store values later on (it seems that, whithout first defining empty Series, the data are not stored properly when retrieving them from the dataframe
3. predefining the legend that has to be shown
4. calculate relative difference between 2 existing columns of the dataframe by calling my function “calculate_rel_diff” and store it in the pandas series
5. make a trace for the data
6. set layout for the figure
Thank you for your help
The relevant code is as follows:
import pandas as pd
import plotly.graph_objs as go
from datetime import datetime as dt
from dictionaries import *
from utilities import *
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
@app.callback(
[Output('content_graph', 'figure')],
[Input('tanks_dropdown', 'value')],
[State('ship_dropdown', 'value')])
def update_image_src(tank, ship):
if tank is not None:
y_axis_title = 'Stock [mton]'
df = pd.read_csv(ship + '_fuel_AMOSvsHIST.csv')
Hist_series = pd.Series([])
tank_abs_diff_series = pd.Series([])
tank_rel_diff_series = pd.Series([])
tank_rel_diff_median_series = pd.Series([])
tank_abs_diff_legend = ""
tank_rel_diff_legend = ""
tank_rel_diff_median_legend = ""
try:
tank_rel_diff_series = calculate_rel_diff(df[Hist_legend + '_diff'], df[tank])
tank_rel_diff_legend = 'Some string'
except:
print("No historian data available")
tank_abs_diff_series = pd.Series([])
tank_rel_diff_series = pd.Series([])
tank_rel_diff_median_series = pd.Series([])
trace = go.Scatter(
x = df["timestamp"],
y = tank_rel_diff_series,
name = tank_rel_diff_legend,
hoverlabel = dict(namelength = -1),
line = dict(
color = ('rgb(75,0,130)'),
width = 2
)
)
data = [trace]
figure = {
'data': data,
'layout': {
'title' : '<b>Tank content</b>',
'paper_bgcolor' : 'rgb(255,255,255)',
'plot_bgcolor' : 'rgb(229,229,229)',
'xaxis' : dict(
gridcolor='rgb(255,255,255)',
showgrid=True,
showline=False,
showticklabels=True,
tickcolor='rgb(127,127,127)',
ticks='outside',
zeroline=False,
title = 'Time'
),
'yaxis' : dict(
gridcolor='rgb(255,255,255)',
showgrid=True,
showline=False,
showticklabels=True,
tickcolor='rgb(127,127,127)',
ticks='outside',
zeroline=False,
title = y_axis_title,
side='left'
)
}
}
return figure