Giving plotly a second try: Trying to update a dropdown

i was able to get a dropdown, with PreventUpdate, using dash :slight_smile:

still a work in progress with the multi=true

I probably have no right to rant, considering my skill level, but i really hate pandas and python lol.

I wonder if any other dataframe library could be easier to use? I read briefly about polars, ibis, spark, duckdb

import dash
from dash import Dash, html, dcc, callback, Output, Input, MATCH, State
from dash.exceptions import PreventUpdate
import pandas as pd
import json
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
from plotly.io import to_html
import os
from os import listdir

path = os.path.join("./static/output/")
fils = [ x for x in os.listdir(path) if x.endswith("csv") ]

options = [{"label": x, "value": x} for x in fils]
app = Dash(__name__)
#app.config.suppress_callback_exceptions = True
app.layout = html.Div([
    html.Div(["CIK:",dcc.Dropdown(id="multidd", multi=True), ]),
    html.Div(["GRAPH:",dcc.Graph(id="dynagraph") ]), ])

@callback(
    Output("multidd", "options"),
    Input("multidd", "search_value"),
    State("multidd", "value"))
def update_multi_options(search_value, value):
    if not search_value:
        raise PreventUpdate
    return [o for o in options if search_value in o["label"] or o["value"] in (value or [])]

@callback(
    Output("dynagraph", "figure"),
    Input("multidd", "value"))
def update_options(search_value):
    if not search_value:
        raise PreventUpdate
    CIK = "{}".format(search_value)
    CIK = CIK.strip("['']")
    print(CIK)
    df = pd.read_csv(f'./static/output/{CIK}')

    dates = df['filing_date'].iloc[:].values[0:]
    concepts = df.columns[0:].values[3:]
    concept = (list(df.iloc[:1, 0]))
    fig = go.Figure().add_trace(go.Scatter(x=df.index,y=df[df.columns[0]],visible=True))
    fig.add_annotation(
        text="<a href='http://localhost:5000' target='_self' style='opacity:50;color:red'>Go Back</a>",
        font=dict(size=20),
        x=0, y=1,
        showarrow=False,
        xref="paper", yref="paper",
        xanchor="left", yanchor="bottom")

    updatemenu = []
    buttons = []
    for col in df.columns[3:]:
        buttons.append(dict(method='restyle',label=col,visible=True,args=[{'y':[df[col]],'x':[df.index],'type':'scatter'}, [0]],))
    mymenu = dict()
    updatemenu.append(mymenu)
    updatemenu[0]['buttons'] = buttons
    updatemenu[0]['direction'] = 'down'
    updatemenu[0]['showactive'] = True
    updatemenu[0]['x'] = 0.5
    updatemenu[0]['xanchor'] = 'center'
    updatemenu[0]['y'] = 1.0
    updatemenu[0]['yanchor'] = 'top'
    updatemenu[0]['font'] = dict(size=20)
    fig.update_layout(showlegend=True, updatemenus=updatemenu, height=900)
#    htmlfig = pio.to_html(fig, full_html=True, include_plotlyjs=True)
    return fig

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

SOURCES:

1 Like