Skip to content

Instantly share code, notes, and snippets.

@pjhoberman
Created March 28, 2020 20:11
Show Gist options
  • Save pjhoberman/9a8a668d472247424f24c2f33f02883e to your computer and use it in GitHub Desktop.
Save pjhoberman/9a8a668d472247424f24c2f33f02883e to your computer and use it in GitHub Desktop.
diff --git a/src/chime_dash/app/components/__init__.py b/src/chime_dash/app/components/__init__.py
index 2956a9f..465578e 100644
--- a/src/chime_dash/app/components/__init__.py
+++ b/src/chime_dash/app/components/__init__.py
@@ -32,6 +32,9 @@ class Body(Component):
"""
"""
super().__init__(language, defaults)
+ print("---")
+ print(LocationComponent())
+ print("---")
self.components = OrderedDict(
navbar=Navbar(language, defaults),
container=Container(language, defaults),
diff --git a/src/chime_dash/app/components/container.py b/src/chime_dash/app/components/container.py
index ec606b7..fbefdac 100644
--- a/src/chime_dash/app/components/container.py
+++ b/src/chime_dash/app/components/container.py
@@ -44,7 +44,9 @@ class Container(Component):
pars = self.components["sidebar"].parse_form_parameters(**kwargs)
kwargs["model"] = SimSirModel(pars)
kwargs["pars"] = pars
-
+ print("~~~")
+ print(kwargs['location'])
+ print(kwargs['hash'])
callback_returns = []
for component in self.components.values():
try:
diff --git a/src/chime_dash/app/components/location.py b/src/chime_dash/app/components/location.py
index 4b66e32..907fc17 100644
--- a/src/chime_dash/app/components/location.py
+++ b/src/chime_dash/app/components/location.py
@@ -12,7 +12,12 @@ class LocationComponent(Component):
callback_inputs = OrderedDict([('location',
CallbackInput(
component_id='location',
- component_property='pathname'))])
+ component_property='pathname')),
+ ('hash',
+ CallbackInput(
+ component_id='location',
+ component_property='hash')),
+ ])
def get_html(self) -> List[ComponentMeta]:
return [Location(id='location')]
diff --git a/src/chime_dash/app/components/sidebar.py b/src/chime_dash/app/components/sidebar.py
index 2da6ffb..80c3b36 100644
--- a/src/chime_dash/app/components/sidebar.py
+++ b/src/chime_dash/app/components/sidebar.py
@@ -73,6 +73,21 @@ class Sidebar(Component):
(key, CallbackInput(component_id=key, component_property="value"))
for key in _INPUTS if _INPUTS[key]["type"] not in ("header", )
)
+ # print("Sidebar")
+
+ @staticmethod
+ def try_int(v):
+ try:
+ return int(v)
+ except ValueError:
+ return v
+
+ @staticmethod
+ def parse_hash(url_hash):
+ params = [param.split("=") for param in url_hash[1:].split(";") if "=" in param]
+ params = {p[0]: Sidebar.try_int(p[1]) for p in params} # this could be done in one step but it gets confusing
+
+ return params
@staticmethod
def parse_form_parameters(**kwargs) -> Tuple[Parameters, Dict[str, Any]]:
@@ -80,8 +95,12 @@ class Sidebar(Component):
Returns Parameters and as_date argument
"""
+ hash_params = kwargs.get('hash', None)
+ if hash_params:
+ hash_params = Sidebar.parse_hash(hash_params)
+
pars = Parameters(
- current_hospitalized=kwargs["current_hospitalized"],
+ current_hospitalized=hash_params.get("current_hospitalized", kwargs["current_hospitalized"]),
doubling_time=kwargs["doubling_time"],
known_infected=kwargs["known_infected"],
market_share=kwargs["market_share"] / 100,
@@ -97,12 +116,18 @@ class Sidebar(Component):
max_y_axis=kwargs["max_y_axis_value"],
n_days=kwargs["n_days"],
)
+ # print("Pars")
+ # print(pars.__dict__)
return pars
def get_html(self) -> List[ComponentMeta]:
"""Initializes the view
"""
elements = []
+ # print("defaults")
+ # print(self.defaults.__dict__)
+ # print("content")
+ # print(self.content)
for idx, data in _INPUTS.items():
if data["type"] == "number":
element = create_number_input(idx, data, self.content, self.defaults)
diff --git a/src/dash_app.py b/src/dash_app.py
index 89975f5..7aaee7c 100644
--- a/src/dash_app.py
+++ b/src/dash_app.py
@@ -33,6 +33,7 @@ app.layout = body.html
app.title = 'Penn Medicine CHIME'
server = app.server
+
@app.callback(body.callback_outputs, list(body.callback_inputs.values()))
def callback(*args): # pylint: disable=W0612
return body.callback(*args)
@@ -42,4 +43,4 @@ def callback(*args): # pylint: disable=W0612
if __name__ == "__main__":
# main()
- app.run_server(host='0.0.0.0')
+ app.run_server(host='0.0.0.0', debug=True, dev_tools_hot_reload_watch_interval=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment