Skip to content

Commit

Permalink
theme to/from json (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-kiaer authored Jan 9, 2020
1 parent 3866a20 commit 8727c4d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
3 changes: 3 additions & 0 deletions webviz_config/_build_webviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions webviz_config/_theme_class.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json


class WebvizConfigTheme:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions webviz_config/_write_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import jinja2

from .themes import installed_themes
from ._config_parser import ConfigParser


Expand All @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion webviz_config/templates/copy_data_template.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ 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

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)
Expand Down
12 changes: 6 additions & 6 deletions webviz_config/templates/webviz_template.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand All @@ -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(
Expand Down

0 comments on commit 8727c4d

Please sign in to comment.