-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c6ed4d
commit 664323f
Showing
6 changed files
with
123 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import inspect | ||
|
||
|
||
def argument_modifier(parent_class, argument_name, modifying_function, args, kwargs): | ||
"""Intelligently modifies input args and kwargs given to a class __init__ function. | ||
* parent_class: The parent class to look for wrt. arguments. | ||
* argument_name: The name of the argument to (potentially) change | ||
* modifying_function: Reference to a function which should take one value. It is | ||
automatically given the current value of the relevant argument | ||
(None if not given), and should return the new modified value. | ||
* args, kwargs: The arguments to be changed. | ||
Returns new pair of args and kwargs. | ||
""" | ||
|
||
arg_index = inspect.getfullargspec(parent_class).args.index(argument_name) | ||
|
||
if len(args) > arg_index: # given as positional argument | ||
args = ( | ||
args[:arg_index] | ||
+ (modifying_function(args[config_arg_index]),) | ||
+ args[arg_index + 1 :] | ||
) | ||
else: | ||
if modifying_function(kwargs.get(argument_name)) is not None: | ||
kwargs[argument_name] = modifying_function(kwargs.get(argument_name)) | ||
|
||
return args, kwargs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import copy | ||
|
||
import dash_html_components as html | ||
|
||
from ._argument_modifier import argument_modifier | ||
|
||
|
||
class FlexBox(html.Div): | ||
"""Behaves like Div from dash_html_components, but extends that container with | ||
flexbox style settings. It also adds necessary flex CSS settings to its children. | ||
Other style settings are untouched. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
def container_style(old_style): | ||
style = {} if old_style is None else copy.deepcopy(old_style) | ||
style.update({"display": "flex", "flexWrap": "wrap"}) | ||
return style | ||
|
||
def update_children_style(children): | ||
if children is not None: | ||
for child in children: | ||
style = ( | ||
copy.deepcopy(child.style) if hasattr(child, "style") else {} | ||
) | ||
|
||
style["flex"] = style.get("flex", "auto") | ||
|
||
if "min-width" not in style and "minWidth" not in style: | ||
style["minWidth"] = "250px" | ||
|
||
child.style = style | ||
|
||
return children | ||
|
||
args, kwargs = argument_modifier( | ||
html.Div, "style", container_style, args, kwargs | ||
) | ||
|
||
args, kwargs = argument_modifier( | ||
html.Div, "children", update_children_style, args, kwargs | ||
) | ||
|
||
super().__init__(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters