I have data from my views.py and I want to pass it using an initial_argument. Is that possible? so that I can use the data to my graph.
in my views.py
@csrf_exempt
def overview(request):
post_data = json.loads(request.body.decode("utf-8"))
value = post_data.get('data')
context = {
'data': value
}
return render(request, 'html', context)
in my template
{% load plotly_dash %}
{% plotly_app name='SimpleExample' initial_arguments=data ratio=1 % }
in my dash app
@app.callback(Output('graph-ph', 'figure'),
[Input('interval-graph-update', 'n_intervals')],
[State('graph-ph', 'figure')])
def update_extend_traces_traceselect(n_intervals, fig_raw, data):
x_new = datetime.datetime.now()
y_new = random.random()
fig_raw['data'][0]['x'] = fig_raw['data'][0]['x'] + [x_new]
fig_raw['data'][0]['y'] = fig_raw['data'][0]['y'] + [y_new]
fig = go.Figure(data=[go.Scatter(
x=fig_raw['data'][0]['x'] + [x_new],
y=fig_raw['data'][0]['y'] + [y_new],
mode='lines+markers',
)
])
fig.update_layout(
title="ph (pH)",
xaxis=dict(
rangeslider=dict(
visible=True
),
)
)
if len(fig_raw['data'][0]['x']) > 20:
fig.update_xaxes(range=[fig_raw['data'][0]['x'][-20],
x_new+datetime.timedelta(seconds=1)], autorange=False)
return go.Figure(fig)