Py.Cafe

jhsmit/

dark-altair

Dynamic Theme Altair Visualization

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import altair as alt
import pandas as pd
import solara
import solara.lab

df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})

chart = (
    alt.Chart(df)
    .mark_point()
    .encode(
        x="x",
        y="y",
    )
)


@solara.component
def Option1():
    with solara.AppBar():
        solara.solara.lab.ThemeToggle()

    dark_effective = solara.lab.use_dark_effective()
    dark_effective_previous = solara.use_previous(dark_effective)

    # uncomment this and the theme toggle no longer works
    solara.Text(str(dark_effective))

    if dark_effective != dark_effective_previous:
        if dark_effective:
            alt.themes.enable("dark")

        else:
            alt.themes.enable("default")

    solara.FigureAltair(chart).key(f"chart-{dark_effective}")

@solara.component
def Option2():
    dark_effective = solara.lab.use_dark_effective()
    if dark_effective:
        options = {"actions": False, "theme": "dark"}
    else:
        options = {"actions": False}
    jchart = alt.JupyterChart.element(chart=chart, embed_options=options)  # type: ignore

@solara.component
def Page():
    with solara.Row():
        Option1()
        Option2()