Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
Fixed typos in docs.
Added some text in inline docs.
Added a function in preparation of a feature allowing
variable names to be referenced in an HTML document.
  • Loading branch information
artscoop committed Jun 9, 2024
1 parent e853eff commit 0328cfc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/dash/htmlayout/builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Build layouts from HTML snippets."""
import logging
import re
import sys
from importlib import import_module
from os import PathLike
from typing import Optional, Type, Any, Union
Expand All @@ -11,6 +11,8 @@

from .converters import evaluate

logger = logging.getLogger("dash.htmlayout")


class Builder:
"""
Expand Down Expand Up @@ -117,10 +119,9 @@ def _autodetect_components(cls):
full_name: str = f"{full_prefix}{tag_name}"
cls._component_registry[full_name] = symbol
except ImportError:
print(
logger.warning(
f"Warning: {module_name} is listed in LayoutBuilder "
f"registry but could not be imported.",
file=sys.stderr,
f"registry but could not be imported."
)

@classmethod
Expand All @@ -131,7 +132,7 @@ def register_library(
Register a new module providing Dash components.
A sensible default is already provided for known libraries,
but you can custom libraries with this method. You just have to
but you can register custom libraries with this method. You just have to
call this by passing a module path and a prefix to apply to detected
components:
Expand All @@ -140,6 +141,13 @@ def register_library(
Builder.register_library("dash_colorful_lib", "colorful")
```
You can then insert components in your HTML files with the
provided prefix, ie.:
```html
<colorful-componentname id="component-id">...
```
"""
if path not in cls._module_registry or replace:
cls._module_registry[path] = prefix
Expand All @@ -165,7 +173,7 @@ def load(self, path: Union[PathLike, str]) -> Component:

def get_component(self, identifier: str) -> Optional[Component]:
"""
Get component instance in the loqded lqyout given an ID.
Get component instance in the loaded layout given an ID.
Args:
identifier: id given to the component.
Expand Down
21 changes: 20 additions & 1 deletion src/dash/htmlayout/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ def evaluate(string: str) -> Any:
Returns:
A Python literal.
"""
return literal_eval(string)


def evaluate_var(name: str) -> Any:
"""
Get a local variable from its name.
Args:
name: local variable name. Must start with "data_".
Returns:
The content of the variable with the name passed as an argument.
Raises:
KeyError: when you give a non-existing name.
ValueError: if the variable name does not start with data_.
"""
if name.startswith("data_"):
return locals()[name]
else:
raise ValueError("Only var names starting with data_ are allowed.")

0 comments on commit 0328cfc

Please sign in to comment.