Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Dash unexpected end of JSON

$
0
0

This program works, but I get a JSON error randomly.
I get random errors when I move the mouse hover quickly.

import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import plotly.graph_objs as go
from dash.dependencies import Input, Output

example1 = np.random.rand(128, 128)
example2 = np.random.rand(128, 128)
example3= np.random.rand(128, 128, 512)
example4 = np.random.rand(128, 128, 512)
example5 = np.random.rand(512)


app = dash.Dash()
app.layout = html.Div([
    html.Div(
        html.H1('example')),
    dcc.Dropdown(
        id = 'select',
        options = [{'value': 'heatmap1', 'label': 'heatmap1'},
                   {'value': 'heatmap2', 'label': 'heatmap2'}],
        value = 'heatmap1'),
    html.Div([
        dcc.Graph(
            id = 'image',
            style = {'width': 600,
                     'display': "inline-block",
                     'verticalAlign': 'top'},
            figure={}),
        dcc.Graph(
            id = 'small',
            style = {'width': 600,
                     'display': 'inline-block',
                     'verticalAlign': 'top'},
            figure={}),
        dcc.Graph(
            id = 'deflection',
            style = {'width': 600,
                     'display': 'inline-block',
                     'verticalAlign': 'top'},
            figure= {})])])


@app.callback(
    Output('small', 'figure'),
    [Input('image', 'hoverData')])
def update(hoverData):
    if hoverData is None:
        raise dash.exceptions.PreventUpdate
    else:
        selectx = hoverData['points'][0]['x']
        selecty = hoverData['points'][0]['y']
        select_example1 = example3[selecty, selectx, :]
        select_example2 = example4[selecty, selectx, :]
        fig1 = go.Figure()
        fig1.add_trace(go.Scatter(
            x = example5,
            y = select_example1,
            mode = 'markers')
        )
        fig1.add_trace(go.Scatter(
            x = example5,
            y = select_example2,
            mode = 'markers')
        )
    return fig1


@app.callback(
    Output('image', 'figure'),
    [Input('select', 'value')])
def mapping(factor):
    fig3 = go.Figure()
    if factor == 'heatmap1':
        fig3.add_trace(go.Heatmap(z = example1))
        fig3.update_layout(yaxis = dict(scaleanchor = 'x'))
    else:
        fig3.add_trace(go.Heatmap(z = example2))
        fig3.update_layout(yaxis = dict(scaleanchor = 'x'))
    return fig3


if __name__ == '__main__':
    app.run_server(debug=True)

error code

Can you tell me if there is a solution?

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 6271

Latest Images

Trending Articles



Latest Images