Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF-2370] Combine library/version into ImportVar and replace _get_imports with _get_imports_list #3198

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Fixes from testing on reflex-web
  • Loading branch information
masenf committed Apr 30, 2024
commit 8643ccb758f99bbd7f9ab405963dc712531e7ca7
4 changes: 2 additions & 2 deletions reflex/components/chakra/navigation/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Link(ChakraComponent):
# If true, the link will open in new tab.
is_external: Var[bool]

def _get_imports(self) -> imports.ImportDict:
return {**super()._get_imports(), **next_link._get_imports()}
def _get_imports_list(self) -> list[imports.ImportVar]:
return [*super()._get_imports_list(), *next_link._get_imports_list()]

@classmethod
def create(cls, *children, **props) -> Component:
Expand Down
35 changes: 16 additions & 19 deletions reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,32 +1593,29 @@ def wrapper(*children, **props) -> CustomComponent:
class NoSSRComponent(Component):
"""A dynamic component that is not rendered on the server."""

def _get_imports(self) -> imports.ImportDict:
def _get_imports_list(self) -> list[ImportVar]:
"""Get the imports for the component.

Returns:
The imports for dynamically importing the component at module load time.
"""
# Next.js dynamic import mechanism.
dynamic_import = {"next/dynamic": [ImportVar(tag="dynamic", is_default=True)]}

# The normal imports for this component.
_imports = super()._get_imports()
return [
*super()._get_imports_list(),
# Next.js dynamic import mechanism.
ImportVar(package="next/dynamic", tag="dynamic", is_default=True),
]

# Do NOT import the main library/tag statically.
if self.library is not None:
_imports[self.library] = [
imports.ImportVar(
tag=None,
render=False,
transpile=self._should_transpile(self.library),
),
]
@property
def import_var(self) -> ImportVar:
"""Will not actually render the tag to import, get it dynamically instead.

return imports.merge_imports(
dynamic_import,
_imports,
)
Returns:
An import var.
"""
imp = super().import_var
imp.tag = None
imp.render = False
return imp

def _get_dynamic_imports(self) -> str:
opts_fragment = ", { ssr: false });"
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/core/debounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def create(cls, *children: Component, **props: Any) -> Component:
)._replace(
_var_type=Type[Component],
merge_var_data=VarData( # type: ignore
imports=child._get_imports(),
imports=child._get_imports_list(),
hooks=child._get_hooks_internal(),
),
),
Expand Down
10 changes: 5 additions & 5 deletions reflex/components/core/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ def render(self) -> Dict:
tag.name = "match"
return dict(tag)

def _get_imports(self) -> imports.ImportDict:
return imports.merge_imports(
super()._get_imports(),
getattr(self.cond._var_data, "imports", {}),
)
def _get_imports_list(self) -> list[imports.ImportVar]:
return [
*super()._get_imports_list(),
*getattr(self.cond._var_data, "imports", []),
]

def _apply_theme(self, theme: Component):
"""Apply the theme to this component.
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/markdown/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _get_imports_list(self) -> list[imports.ImportVar]:
is_default=True,
),
ImportVar(
package="remark-katex@6.0.3",
package="rehype-katex@6.0.3",
tag=_REHYPE_KATEX._var_name,
is_default=True,
),
Expand Down
18 changes: 6 additions & 12 deletions reflex/components/radix/primitives/accordion.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,12 @@ def _apply_theme(self, theme: Component):
accordion_theme_root
)

def _get_imports(self):
return imports.merge_imports(
super()._get_imports(),
self._var_data.imports if self._var_data else {},
{"@emotion/react": [imports.ImportVar(tag="keyframes")]},
)
def _get_imports_list(self) -> list[imports.ImportVar]:
return [
*super()._get_imports_list(),
*(self._var_data.imports if self._var_data else {}),
imports.ImportVar(package="@emotion/react", tag="keyframes"),
]

def get_event_triggers(self) -> Dict[str, Any]:
"""Get the events triggers signatures for the component.
Expand Down Expand Up @@ -644,12 +644,6 @@ def create(cls, *children, **props) -> Component:
def _apply_theme(self, theme: Component):
self.style = Style({**self.style})

# def _get_imports(self):
# return {
# **super()._get_imports(),
# "@emotion/react": [imports.ImportVar(tag="keyframes")],
# }


class Accordion(ComponentNamespace):
"""Accordion component."""
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/radix/themes/typography/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class Link(RadixThemesComponent, A, MemoizationLeaf):
# If True, the link will open in a new tab
is_external: Var[bool]

def _get_imports(self) -> imports.ImportDict:
return {**super()._get_imports(), **next_link._get_imports()}
def _get_imports_list(self) -> list[imports.ImportVar]:
return [*super()._get_imports_list(), *next_link._get_imports_list()]

@classmethod
def create(cls, *children, **props) -> Component:
Expand Down
3 changes: 2 additions & 1 deletion reflex/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
List,
Literal,
Optional,
Sequence,
Tuple,
Type,
Union,
Expand Down Expand Up @@ -129,7 +130,7 @@ class VarData(Base):
def __init__(
self,
imports: ImportList
| List[ImportVar | Dict[str, Optional[Union[str, bool]]]]
| Sequence[ImportVar | Dict[str, Optional[Union[str, bool]]]]
| ImportDict
| Dict[str, set[ImportVar]]
| None = None,
Expand Down
3 changes: 2 additions & 1 deletion reflex/vars.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ from typing import (
Iterable,
List,
Optional,
Sequence,
Set,
Tuple,
Type,
Expand All @@ -41,7 +42,7 @@ class VarData(Base):
def __init__(
self,
imports: ImportList
| List[ImportVar | Dict[str, Optional[Union[str, bool]]]]
| Sequence[ImportVar | Dict[str, Optional[Union[str, bool]]]]
| ImportDict
| Dict[str, set[ImportVar]]
| None = None,
Expand Down
Loading