Skip to content

Commit

Permalink
fix: theme not stored over hot reload
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Mar 8, 2024
1 parent 0b0d465 commit 7fe9e73
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
11 changes: 11 additions & 0 deletions solara/lab/components/theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def _set_theme(themes: Union[Dict[str, Dict[str, str]], None]):
setattr(widget, k, v)


def _get_theme(theme: Theme) -> Dict[str, Dict[str, str]]:
theme_dict: Dict[str, Dict[str, str]] = cast(Dict[str, Dict[str, str]], {})
for theme_type, theme_value in theme.themes.__dict__.items():
theme_traits = theme_value.keys
theme_dict[theme_type] = {}
for trait in theme_traits:
if not trait.startswith("_"):
theme_dict[theme_type][trait] = getattr(theme_value, trait)
return theme_dict


@component_vue("theming.vue")
def _ThemeToggle(
theme_dark: str,
Expand Down
29 changes: 19 additions & 10 deletions solara/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ def load_app_widget(app_state, app_script: AppScript, pathname: str):
container.children = [widget]


def load_themes(themes: Dict[str, Dict[str, str]], dark: bool):
# While these usually gets set from the frontend, in solara (server) we want to know theme information directly at the first
# render. Also, using the same trait allows us to write code which works on all widgets platforms, instead
# or using something different when running under solara server
from solara.lab.components.theming import _set_theme, theme

_set_theme(themes)
theme.dark_effective = dark


def solara_comm_target(comm, msg_first):
app: Optional[AppScript] = None

Expand All @@ -368,20 +378,13 @@ def on_msg(msg):
app_name = args.get("appName") or "__default__"
app = apps[app_name]
context = kernel_context.get_current_context()
dark = args.get("dark", False)
themes = args.get("themes", None)
import ipyvuetify

from solara.lab.components.theming import _set_theme, theme

_set_theme(themes)
# While this usually gets set from the frontend, in solara (server) we want to know this directly at the first
# render. Also, using the same trait allows us to write code which works on all widgets platforms, instead
# or using something different when running under solara server
theme.dark_effective = dark

container = ipyvuetify.Html(tag="div")
context.container = container
themes = args.get("themes")
dark = args.get("dark")
load_themes(themes, dark)
load_app_widget(None, app, path)
comm.send({"method": "finished", "widget_id": context.container._model_id})
elif method == "app-status":
Expand All @@ -395,11 +398,17 @@ def on_msg(msg):
comm.send({"method": "app-status", "started": False})

elif method == "reload":
from solara.lab.components.theming import _get_theme, theme

assert app is not None
context = kernel_context.get_current_context()
path = data.get("path", "")
current_theme = theme._instance.value
theme_dict = _get_theme(current_theme)

with context:
context.restart()
load_themes(theme_dict, current_theme.dark_effective)
load_app_widget(context.state, app, path)
comm.send({"method": "finished"})
else:
Expand Down

0 comments on commit 7fe9e73

Please sign in to comment.