Skip to content

Commit

Permalink
feat: support theme toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhsmit committed Feb 26, 2024
1 parent 1edd6e6 commit 7dad899
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions examples/solara_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,34 @@
theme = solara.reactive("light")


def make_molstar():
print("make molstar")
c = PDBeMolstar(molecule_id=molecule_id.value, theme=theme.value)
return c
def use_trait_observe(has_trait_object, name):
# TODO: this hook should go into solara
counter = solara.use_reactive(0)
counter.get() # make the main component depend on this counter

def connect():
def update(change):
counter.value += 1

has_trait_object.observe(update, name)

def cleanup():
has_trait_object.unobserve(update, name)

return cleanup

solara.use_effect(connect, [])
return getattr(has_trait_object, name)


def use_dark_effective():
return use_trait_observe(solara.lab.theme, "dark_effective")


# def make_molstar():
# print("make molstar")
# c = PDBeMolstar(molecule_id=molecule_id.value, theme=theme.value)
# return c


def rand_rgb() -> dict[str, int]:
Expand Down Expand Up @@ -43,7 +67,19 @@ def gen_color_data():

@solara.component
def Page():
c = solara.use_memo(make_molstar, dependencies=[molecule_id.value, theme.value])
with solara.AppBar():
solara.lab.ThemeToggle()

dark_effective = use_dark_effective()
theme_value = "dark" if dark_effective else "light"
print("theme_value", theme_value)

def make_molstar():
print("make molstar")
c = PDBeMolstar(molecule_id=molecule_id.value, theme=theme_value)
return c

c = solara.use_memo(make_molstar, dependencies=[molecule_id.value, theme_value])

def set_spin(value):
# doesnt work, triggers rerender instead of setting spin
Expand Down Expand Up @@ -97,7 +133,7 @@ def toggle_spin():
value=theme.value,
on_value=theme.set,
values=["light", "dark"],
)
) # works
solara.Text(
f"molecule_id: {molecule_id.value}, count: {count.value}, spin: {spin.value}, {c.spin}"
)
Expand Down

0 comments on commit 7dad899

Please sign in to comment.