Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

Plotly express bar chart yields server error

In a dash callback I load a simple dataframe in a ploty express bar chart.
In dataframe df I have columns ‘hour’ and ‘count’. I have 7 rows.
When using:
fig = px.bar(df, x=‘hour’, y=‘count’) I get a server error. Not always though.
Anyone knows what this could be?
With a standard fig I do not get the error.

Hey @Bastituta,
Can you show us the traceback of code? and please show the full code so that we can see what you are doing and what you are expecting?

Thanks for helping me!
It is quite a lot of code. I took out the relevant parts.
I do not know how to figure out what the problem is.
Just before px.bar I am able to print things, so that works okay.
Actually, px.bar sometimes loads, but oftne doesn’t.
Sorry for the bad appearance, I do not know how to save code as markdown (I am not a programmer).

Here is part of the code:

necessary input

import dash
import dash_core_components as dcc
from dash.dependencies import Input, Output, State, ALL
import dash_html_components as html
from dash.exceptions import PreventUpdate
from app import app
import dash_leaflet as dl
import plotly.express as px
import pandas as pd
import spatialite

part of the layout

    html.Div([
        dl.Map([dl.TileLayer(url=Stadia_AlidadeSmoothDark), dl.Pane([dl.LayerGroup(id="container3", children=[])], style ={"z-index": 203}),
        dl.Pane([dl.LayerGroup(id="container2", children=[])], style ={"z-index": 202}),
        dl.Pane([dl.LayerGroup(id="container1", children=[])], style ={"z-index": 201})],
        id="map", bounds=image_bounds, style={'width': '90%', 'height': '80vh', 'margin-top': "20px", "display": "block"}),

the relevant callback

@app.callback([Output(“container3”, “children”),Output(“grafiek”, “figure”),Output(“totaal”, “children”)],[Input({‘type’: ‘basismarkers’, ‘index’: ALL}, ‘n_clicks’)])
def select_marker(*args):

selmarkers = []
triggered = dash.callback_context.triggered

if len(triggered) > 1 or (len(triggered) == 1 and triggered[0]['value'] == 0):
    raise PreventUpdate

conn = spatialite.connect(sqlitepath)

marker_id = triggered[0]["prop_id"].split(":")[1][1:7]
df0 =pd.read_sql('''SELECT latitude, longitude from herkomsten_REF_O where PC6 = "''' + marker_id + '''"''', conn)
lat = df0.iloc[0]['latitude']
lon = df0.iloc[0]['longitude']

selmarker = dl.Marker(children=dl.Tooltip(marker_id),position=[lat,lon],icon=icon_selected,
   id="marker2" + marker_id)
selmarkers.append(selmarker)

df1 = pd.read_sql('''select * from HBs_REF_O where PC6 ="''' + marker_id + '''"''', conn)

for index, row in df1.iterrows():
    marker = dl.Marker(children=dl.Tooltip(row["PC6_2"]),position=[row["latitude"], row["longitude"]],icon=icon_destinations,
       id="marker2" + row['PC6_2'])
    selmarkers.append(marker)

df = pd.read_sql('''select * from tijden_REF_O where PC6 ="''' + marker_id + '''"''', conn)


try:
    totaal = df.groupby(['PC6'])['count'].sum().iloc[0]
    tot = 'Totaal voor ' + marker_id + ': {}'.format(totaal)
except:
    tot =''

# from here it goes sometimes wrong
fig = px.bar(df0, x='uur', y='count', color = 'typerit', title= 'Aankomsten en vertrekken', range_x=[0,23],
             labels={"count": "ritten" }, hover_data=['count'], barmode = 'stack')

del df0, df1, df

conn.close()
return selmarkers, fig, tot

The relevant dataframe looks like this:
df = pd.read_sql(’’‘select * from tijden_REF_O where PC6 ="’’’ + marker_id + ‘’’"’’’, conn)

index     PC6  uur  count   typerit

0 647 7382SB 13 1 vertrek
1 648 7382SB 14 14 vertrek
2 649 7382SB 15 4 vertrek
3 650 7382SB 16 26 vertrek
4 651 7382SB 17 17 vertrek
5 1492 7382SB 7 1 aankomst
6 1493 7382SB 8 1 aankomst
7 1494 7382SB 9 48 aankomst

I was asking for error you get.

Thanks!
I do not know how to find the error. Don’t know how to open the browser’s console.
I get the following message:
image.png