Skip to content

Instantly share code, notes, and snippets.

@psorianom
Last active April 23, 2020 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psorianom/c60c22b976f7e8b83121ed4892aa2ffd to your computer and use it in GitHub Desktop.
Save psorianom/c60c22b976f7e8b83121ed4892aa2ffd to your computer and use it in GitHub Desktop.
Serialization/Deserialization of Dash components
from importlib import import_module
from pprint import pprint
from typing import List, Dict
from dash.development.base_component import Component
from dash_html_components import Div
from dash_html_components import P ,Mark
from dash_interface.helper import serialize_components
def load_class(class_namespace, class_type):
try:
module = import_module(class_namespace)
class_ = getattr(module, class_type)
return class_
except:
print(f"Cannot import {class_namespace}:{class_type}. Is it installed?")
return None
pass
def deserialize_components(component_dict: Dict):
class_type = component_dict["type"]
class_namespace = component_dict["namespace"].split("/")[0]
# Try loading it
class_ = load_class(class_namespace, class_type)
if not class_:
print(f"Cannot continue.")
return None
# we begin deserializing
component_children = component_dict["props"]["children"]
if isinstance(component_children, str):
return class_(**component_dict["props"])
elif isinstance(component_children, list):
deserialized_children = []
for child in component_children:
if isinstance(child, str):
deserialized_children.append(child)
elif isinstance(child, dict):
deserialized_children.append(deserialize_components(child))
component_dict["props"]["children"] = deserialized_children
return class_(**component_dict["props"])
def serialize_components(component: Component):
component_dict = component.to_plotly_json()
component_children = component_dict["props"]["children"]
if isinstance(component_children, str):
return component_dict
elif not isinstance(component_children, list):
component_children = [component_children]
serialized_children = []
for child in component_children:
if isinstance(child, str):
serialized_children.append(child)
elif isinstance(child, Component):
serialized_children.append(serialize_components(child))
component_dict["props"]["children"] = serialized_children
return component_dict
# Serializing
list_things = [P("Hola!"),
P(["Mme", Mark("Christelle", className="wadevs"), " ", Mark("Nomi", className="wadevs")]),
P("demeurant")]
list1 = []
for l in list_things:
result = serialize_components(l)
lista1.append(result)
pprint(list1)
# Deserializing
components_string = """
[{'namespace': 'dash_html_components',
'props': {'children': 'Hola!'},
'type': 'P'},
{'namespace': 'dash_html_components',
'props': {'children': ['Mmme',
{'namespace': 'dash_html_components',
'props': {'children': 'Christelle',
'className': 'wadevs'},
'type': 'Mark'},
' ',
{'namespace': 'dash_html_components',
'props': {'children': 'Nomi', 'className': 'wadevs'},
'type': 'Mark'}]},
'type': 'P'},
{'namespace': 'dash_html_components',
'props': {'children': 'demeurant'},
'type': 'P'}]
"""
serialized_components = eval(components_string)
deserialized_components = []
for elem in serialized_components:
test = deserialize_html_components(elem)
deserialized_components.append(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment