AnnMarieW / dash-bootstrap-templates

A collection of 52 Plotly figure templates with a Bootstrap theme. Two theme switch components. Stylesheet to apply Bootstrap themes to Plotly Dash components.
https://hellodash.pythonanywhere.com/
MIT License
135 stars 25 forks source link

The most recent version (1.2.4) started to cause an import error #41

Open alex11818 opened 2 months ago

alex11818 commented 2 months ago

The most recent version of dash_bootstrap_templates started to cause an error on importing:

from dash_bootstrap_templates import load_figure_template, template_from_url, ThemeChangerAIO

The error itself seems to originate in ThemeChangerAIO. The error message: ... aio_theme_switch.py", line 8, in <module> class ThemeSwitchAIO(html.Div): File "..\lib\site-packages\aio\aio_theme_switch.py", line 41, in ThemeSwitchAIO themes: Union[tuple[str, str], list[str]] = (dbc.themes.CYBORG, dbc.themes.BOOTSTRAP), TypeError: 'type' object is not subscriptable

Python version: 3.8.19 Library version: dash-bootstrap-templates 1.2.4 pyhd8ed1ab_0 conda-forge

AnnMarieW commented 2 months ago

Hi @alex11818

I can't replicate the issue. I don't use conda, but I just ran this minimal example and it worked fine. Could you try running this app?


from dash import Dash, dcc, html, Input, Output
import pandas as pd
import plotly.express as px
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import ThemeChangerAIO, template_from_url

dbc_css = (
    "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates@V1.0.1/dbc.min.css"
)
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP, dbc_css])

df = pd.DataFrame(
    {
        "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
        "Amount": [4, 1, 2, 2, 4, 5],
        "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"],
    }
)
header = html.H4(
    "ThemeChangerAIO Demo", className="bg-primary text-white p-4 mb-2 text-center"
)
buttons = html.Div(
    [
        dbc.Button("Primary", color="primary"),
        dbc.Button("Secondary", color="secondary"),
        dbc.Button("Success", color="success"),
        dbc.Button("Warning", color="warning"),
        dbc.Button("Danger", color="danger"),
        dbc.Button("Info", color="info"),
        dbc.Button("Light", color="light"),
        dbc.Button("Dark", color="dark"),
        dbc.Button("Link", color="link"),
    ],
    className="m-4",
)

graph = html.Div(dcc.Graph(id="graph"), className="m-4")

app.layout = dbc.Container(
    [
        header,
        dbc.Row(
            [
                dbc.Col(
                    ThemeChangerAIO(
                        aio_id="theme", radio_props={"value": dbc.themes.FLATLY}
                    ),
                    width=2,
                ),
                dbc.Col([buttons, graph], width=10),
            ]
        ),
    ],
    className="m-4 dbc",
    fluid=True,
)

@app.callback(
    Output("graph", "figure"), Input(ThemeChangerAIO.ids.radio("theme"), "value"),
)
def update_graph_theme(theme):
    return px.bar(
        df,
        x="Fruit",
        y="Amount",
        color="City",
        barmode="group",
        template=template_from_url(theme),
    )

if __name__ == "__main__":
    app.run_server(debug=True)
alex11818 commented 2 months ago

Hi @alex11818

I can't replicate the issue. I don't use conda, but I just ran this minimal example and it worked fine. Could you try running this app?


from dash import Dash, dcc, html, Input, Output
import pandas as pd
import plotly.express as px
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import ThemeChangerAIO, template_from_url

dbc_css = (
    "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates@V1.0.1/dbc.min.css"
)
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP, dbc_css])

df = pd.DataFrame(
    {
        "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
        "Amount": [4, 1, 2, 2, 4, 5],
        "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"],
    }
)
header = html.H4(
    "ThemeChangerAIO Demo", className="bg-primary text-white p-4 mb-2 text-center"
)
buttons = html.Div(
    [
        dbc.Button("Primary", color="primary"),
        dbc.Button("Secondary", color="secondary"),
        dbc.Button("Success", color="success"),
        dbc.Button("Warning", color="warning"),
        dbc.Button("Danger", color="danger"),
        dbc.Button("Info", color="info"),
        dbc.Button("Light", color="light"),
        dbc.Button("Dark", color="dark"),
        dbc.Button("Link", color="link"),
    ],
    className="m-4",
)

graph = html.Div(dcc.Graph(id="graph"), className="m-4")

app.layout = dbc.Container(
    [
        header,
        dbc.Row(
            [
                dbc.Col(
                    ThemeChangerAIO(
                        aio_id="theme", radio_props={"value": dbc.themes.FLATLY}
                    ),
                    width=2,
                ),
                dbc.Col([buttons, graph], width=10),
            ]
        ),
    ],
    className="m-4 dbc",
    fluid=True,
)

@app.callback(
    Output("graph", "figure"), Input(ThemeChangerAIO.ids.radio("theme"), "value"),
)
def update_graph_theme(theme):
    return px.bar(
        df,
        x="Fruit",
        y="Amount",
        color="City",
        barmode="group",
        template=template_from_url(theme),
    )

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

Hi @AnnMarieW! Thanks for the quick reply! I have tried to run this app and got the same error. Though the app runs fine in my old environment (dash-bootstrap-templates=1.0.8).

AnnMarieW commented 2 months ago

Hi @alex11818

Sorry it's not working for you. It might be that the conda version is out of date? There was an error like that recently, but it was fixed.

Take a look at this live version on pycafe: https://py.cafe/amward/dash-theme-changer-demo

alex11818 commented 2 months ago

@AnnMarieW,

My conda version is not that old (24.5.0), so I don't think the conda version causes the issue (though I wouldn't vouch for that).

Just in case, I have set up and tested two environments with:

  1. dash-bootstrap-templates=1.2.0 - I got the same error
  2. dash-bootstrap-templates=1.0.8 - runs OK.

Thanks for sharing the demo. I see it runs fine on 1.2.4. So, I am a bit puzzled regarding the cause. But, in the worst case, I can fall back to 1.0.8 :)

I understand that it may be hard to figure out the cause. But thanks for taking a look at the issue!

Also, it is totally up to you to keep or close the issue. I would keep it for some time; probably someone else has experienced the same problem.

AnnMarieW commented 2 months ago

Ok, I'll keep it open for a bit to make it a little easier to find.

The issue is that conda version you are using is not using the latest version of this library. I don't think there is anything I can do since this bug was fixed as of 1.2.4