Iâve have something weird going on here⌠Sometimes my code works perfectly fine but sometimes I get an invalid value error on the color of the plotly figures. One time on the âcontinentâ and the other time on the âcountryâ and one time it just workst perfectly. As I delete the second figure I think the error dissapears. Someone who can help me?
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State, ALL, MATCH
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from Hotspot_App_Local import app
from Cards import Cards
df = pd.read_csv(âmerged_layers.csvâ)
scatterlst = dcc.Dropdown(id=âscatterglobalâ, options= [{âlabelâ: col, âvalueâ: col} for col in sorted(df.columns)], multi=True)
card1 = Cards(âGlobal Analysisâ, [scatterlst], Header= True)
scatterlst2 = dcc.Dropdown(id=âscattercontâ, options= [{âlabelâ: col, âvalueâ: col} for col in sorted(df.columns)], multi=True)
continents = sorted(df[âcontinentâ].unique())
continent_drop = dcc.Dropdown(id=âcontlstâ, options= [{âlabelâ: cont, âvalueâ: cont} for cont in continents], value= continents[0])
excl_country = dcc.Dropdown(id=âexcl_countryâ, multi= True)
card2 = Cards(âSelect Layersâ, [scatterlst2], Header= True)
card3 = Cards(âSelect Continentâ, [continent_drop], Header= True)
card4 = Cards(âExclude Countriesâ, [excl_country], Header= True)
layout = html.Div([
dbc.Card(card1, color=âlightâ, outline=True),
html.Div(id= âglobalplotdivâ),
dbc.CardHeader(âContinental Analysisâ),
html.Br(),
dbc.Row([dbc.Col(dbc.Card(card2, color=âlightâ, outline=True)),
dbc.Col(dbc.Card(card3, color=âlightâ, outline=True)),
dbc.Col(dbc.Card(card4, color=âlightâ, outline=True))
]),
html.Div(id= âcontplotdivâ)
])
@app.callback(
Output(âscatterglobalâ, âvalueâ),
[Input(âselected_columnsâ, âdataâ)]
)
def set_multiselect(data):
return data
@app.callback(
Output(âglobalplotdivâ, âchildrenâ),
[Input(âscatterglobalâ, âvalueâ)]
)
def global_scatterplot(columns):
df = pd.read_csv(âmerged_layers.csvâ)
if len(columns) > 2:
fig = px.scatter_matrix(df,
dimensions= columns,
color= df[âcontinentâ])
fig.update_traces(diagonal_visible=False, showupperhalf=False)
return dcc.Graph(id=âworld_scatterâ, figure=fig)
if len(columns) == 2:
fig = px.scatter(df,
x= columns[0],
y= columns[1],
color= df['continent']
)
return dcc.Graph(id='world_scatter', figure=fig)
else:
return dcc.ConfirmDialog(id='global_warning', message="Select at least Two Layers", )
@app.callback(
Output(âglobal_warningâ, âdisplayedâ),
[Input(âscatterglobalâ, âvalueâ)]
)
def warning(columns):
if len(columns) <= 1:
return True
return False
@app.callback(
Output(âscattercontâ, âvalueâ),
[Input(âscatterglobalâ, âvalueâ)]
)
def set_multiselect2(data):
return data
@app.callback(
Output(âexcl_countryâ, âoptionsâ),
[Input(âcontlstâ, âvalueâ)]
)
def excl_country(cont):
df = pd.read_csv(âmerged_layers.csvâ)
df = df[df[âcontinentâ] == cont]
return [{âlabelâ: c, âvalueâ: c} for c in sorted(df[âcountryâ].unique())]
@app.callback(
Output(âcontplotdivâ, âchildrenâ),
[Input(âscattercontâ, âvalueâ),
Input(âcontlstâ, âvalueâ),
Input(âexcl_countryâ, âvalueâ)]
)
def cont_scatterplot(columns, cont, ex_country):
df2 = pd.read_csv(âmerged_layers.csvâ)
df2 = df2[df2[âcontinentâ] == cont]
if ex_country is not None:
mask = df2[âcountryâ].isin(ex_country)
df2 = df[~mask]
if len(columns) > 2:
fig = px.scatter_matrix(df2,
dimensions= columns,
color= df2[âcountryâ])
fig.update_traces(diagonal_visible=False, showupperhalf=False)
return dcc.Graph(id='cont_scatter', figure=fig)
if len(columns) == 2:
fig = px.scatter(df2,
x= columns[0],
y= columns[1],
color= df2['country']
)
return dcc.Graph(id= 'cont_scatter', figure=fig)
else:
return dcc.ConfirmDialog(id='cont_warning', message="Select at least Two Layers", )
@app.callback(
Output(âcont_warningâ, âdisplayedâ),
[Input(âcontlstâ, âvalueâ)]
)
def warning(columns):
if len(columns) <= 1:
return True
return False