diff --git a/webviz_config/_build_webviz.py b/webviz_config/_build_webviz.py index ba66aa5f..f1fa96be 100644 --- a/webviz_config/_build_webviz.py +++ b/webviz_config/_build_webviz.py @@ -36,6 +36,9 @@ def build_webviz(args): for asset in installed_themes[args.theme].assets: shutil.copy(asset, os.path.join(build_directory, "assets")) + with open(os.path.join(build_directory, "theme_settings.json"), "w") as filehandle: + filehandle.write(installed_themes[args.theme].to_json()) + try: if args.portable: print( diff --git a/webviz_config/_theme_class.py b/webviz_config/_theme_class.py index 22dcd280..d6aac12a 100644 --- a/webviz_config/_theme_class.py +++ b/webviz_config/_theme_class.py @@ -1,4 +1,5 @@ import copy +import json class WebvizConfigTheme: @@ -46,6 +47,13 @@ def __init__(self, theme_name): self._assets = [] self._plotly_theme = {} + def to_json(self): + return json.dumps(vars(self), indent=4, sort_keys=True) + + def from_json(self, json_string): + for key, value in json.loads(json_string).items(): + setattr(self, key, value) + def adjust_csp(self, dictionary, append=True): """If the default CSP settings needs to be changed, this function can be called by giving in a dictionary with key-value pairs which should diff --git a/webviz_config/_write_script.py b/webviz_config/_write_script.py index 58088b86..d571513a 100644 --- a/webviz_config/_write_script.py +++ b/webviz_config/_write_script.py @@ -5,7 +5,6 @@ import jinja2 -from .themes import installed_themes from ._config_parser import ConfigParser @@ -18,11 +17,7 @@ def write_script(args, build_directory, template_filename, output_filename): configuration["portable"] = args.portable is not None configuration["config_folder"] = repr(pathlib.Path(args.yaml_file).resolve().parent) - theme = installed_themes[args.theme] - configuration["csp"] = theme.csp - configuration["feature_policy"] = theme.feature_policy - configuration["external_stylesheets"] = theme.external_stylesheets - configuration["theme"] = args.theme + configuration["theme_name"] = args.theme configuration["author"] = getpass.getuser() configuration["current_date"] = datetime.date.today().strftime("%Y-%m-%d") diff --git a/webviz_config/templates/copy_data_template.py.jinja2 b/webviz_config/templates/copy_data_template.py.jinja2 index 1bf61d92..6f0ad6c6 100644 --- a/webviz_config/templates/copy_data_template.py.jinja2 +++ b/webviz_config/templates/copy_data_template.py.jinja2 @@ -21,6 +21,9 @@ import {{ module[0] }} as {{ module[1] }} {%- endif %} {% endfor %} +theme = webviz_config.WebvizConfigTheme("{{ theme_name }}") +theme.from_json((Path(__file__).resolve().parent / "theme_settings.json").read_text()) + app = dash.Dash() app.config.suppress_callback_exceptions = True @@ -28,7 +31,7 @@ app.webviz_settings = { "shared_settings": webviz_config.SHARED_SETTINGS_SUBSCRIPTIONS.transformed_settings( {{ shared_settings }}, {{ config_folder }}, False ), - "theme": installed_themes["{{ theme }}"], + "theme": theme, } CACHE.init_app(app.server) diff --git a/webviz_config/templates/webviz_template.py.jinja2 b/webviz_config/templates/webviz_template.py.jinja2 index 7bf06729..bf8b9af1 100644 --- a/webviz_config/templates/webviz_template.py.jinja2 +++ b/webviz_config/templates/webviz_template.py.jinja2 @@ -53,7 +53,10 @@ logging.config.dictConfig( } ) -app = dash.Dash(__name__, external_stylesheets={{ external_stylesheets }}) +theme = webviz_config.WebvizConfigTheme("{{ theme_name }}") +theme.from_json((Path(__file__).resolve().parent / "theme_settings.json").read_text()) + +app = dash.Dash(__name__, external_stylesheets=theme.external_stylesheets) server = app.server app.title = "{{ title }}" @@ -64,15 +67,12 @@ app.webviz_settings = { {{ shared_settings }}, {{ config_folder }}, {{ portable }} ), "portable": {{ portable }}, - "theme": installed_themes["{{ theme }}"], + "theme": theme, } CACHE.init_app(server) -CSP = {{ csp }} -FEATURE_POLICY = {{ feature_policy }} - -Talisman(server, content_security_policy=CSP, feature_policy=FEATURE_POLICY) +Talisman(server, content_security_policy=theme.csp, feature_policy=theme.feature_policy) WEBVIZ_STORAGE.use_storage = {{portable}} WEBVIZ_STORAGE.storage_folder = path.join(