Revert fstrings change (#895)
This commit is contained in:
parent
5674d9a7a0
commit
60323a3aec
@ -263,7 +263,6 @@ class App(Base):
|
|||||||
# Generate the component if it is a callable.
|
# Generate the component if it is a callable.
|
||||||
try:
|
try:
|
||||||
component = component if isinstance(component, Component) else component()
|
component = component if isinstance(component, Component) else component()
|
||||||
component.set_state(self.state)
|
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
message = str(e)
|
message = str(e)
|
||||||
if "BaseVar" in message or "ComputedVar" in message:
|
if "BaseVar" in message or "ComputedVar" in message:
|
||||||
|
@ -27,8 +27,4 @@ class Bare(Component):
|
|||||||
return cls(contents=str(contents)) # type: ignore
|
return cls(contents=str(contents)) # type: ignore
|
||||||
|
|
||||||
def _render(self) -> Tag:
|
def _render(self) -> Tag:
|
||||||
contents = str(self.contents)
|
return Tagless(contents=str(self.contents))
|
||||||
if self.state is not None:
|
|
||||||
check = f"{{{self.state.get_name()}"
|
|
||||||
contents = str(self.contents).replace(check, f"${check}")
|
|
||||||
return Tagless(contents=contents)
|
|
||||||
|
@ -24,9 +24,6 @@ from pynecone.style import Style
|
|||||||
from pynecone.utils import format, imports, path_ops, types
|
from pynecone.utils import format, imports, path_ops, types
|
||||||
from pynecone.var import BaseVar, Var
|
from pynecone.var import BaseVar, Var
|
||||||
|
|
||||||
if typing.TYPE_CHECKING:
|
|
||||||
from pynecone.state import State
|
|
||||||
|
|
||||||
|
|
||||||
class Component(Base, ABC):
|
class Component(Base, ABC):
|
||||||
"""The base class for all Pynecone components."""
|
"""The base class for all Pynecone components."""
|
||||||
@ -37,9 +34,6 @@ class Component(Base, ABC):
|
|||||||
# The style of the component.
|
# The style of the component.
|
||||||
style: Style = Style()
|
style: Style = Style()
|
||||||
|
|
||||||
# The app state the component is connected to.
|
|
||||||
state: Optional[Type[State]] = None
|
|
||||||
|
|
||||||
# A mapping from event triggers to event chains.
|
# A mapping from event triggers to event chains.
|
||||||
event_triggers: Dict[str, Union[EventChain, Var]] = {}
|
event_triggers: Dict[str, Union[EventChain, Var]] = {}
|
||||||
|
|
||||||
@ -120,7 +114,7 @@ class Component(Base, ABC):
|
|||||||
if types._issubclass(field_type, Var):
|
if types._issubclass(field_type, Var):
|
||||||
try:
|
try:
|
||||||
# Try to create a var from the value.
|
# Try to create a var from the value.
|
||||||
kwargs[key] = Var.create(value, is_string=type(value) == str)
|
kwargs[key] = Var.create(value)
|
||||||
|
|
||||||
# Check that the var type is not None.
|
# Check that the var type is not None.
|
||||||
if kwargs[key] is None:
|
if kwargs[key] is None:
|
||||||
@ -365,7 +359,7 @@ class Component(Base, ABC):
|
|||||||
children = [
|
children = [
|
||||||
child
|
child
|
||||||
if isinstance(child, Component)
|
if isinstance(child, Component)
|
||||||
else Bare.create(contents=Var.create(child))
|
else Bare.create(contents=Var.create(child, is_string=True))
|
||||||
for child in children
|
for child in children
|
||||||
]
|
]
|
||||||
return cls(children=children, **props)
|
return cls(children=children, **props)
|
||||||
@ -399,19 +393,6 @@ class Component(Base, ABC):
|
|||||||
child.add_style(style)
|
child.add_style(style)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def set_state(self, state: Type[State]):
|
|
||||||
"""Set the state of the component and its children.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
state: The state to set.
|
|
||||||
"""
|
|
||||||
# Set the state of the component.
|
|
||||||
self.state = state
|
|
||||||
|
|
||||||
# Set the state of the children.
|
|
||||||
for child in self.children:
|
|
||||||
child.set_state(state)
|
|
||||||
|
|
||||||
def render(self) -> str:
|
def render(self) -> str:
|
||||||
"""Render the component.
|
"""Render the component.
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pynecone.components.base.bare import Bare
|
from pynecone.components.base.bare import Bare
|
||||||
from pynecone.state import DefaultState
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -9,7 +8,7 @@ from pynecone.state import DefaultState
|
|||||||
[
|
[
|
||||||
("hello", "hello"),
|
("hello", "hello"),
|
||||||
("{}", "{}"),
|
("{}", "{}"),
|
||||||
("{default_state.name}", "${default_state.name}"),
|
("${default_state.name}", "${default_state.name}"),
|
||||||
("{state.name}", "{state.name}"),
|
("{state.name}", "{state.name}"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -21,5 +20,4 @@ def test_fstrings(contents, expected):
|
|||||||
expected: The expected output.
|
expected: The expected output.
|
||||||
"""
|
"""
|
||||||
comp = Bare.create(contents)
|
comp = Bare.create(contents)
|
||||||
comp.set_state(DefaultState)
|
assert str(comp) == expected
|
||||||
assert str(comp) == f"{{`{expected}`}}"
|
|
||||||
|
@ -53,7 +53,7 @@ def test_upload_component_render(upload_component):
|
|||||||
str(upload_component) == f"<ReactDropzone multiple={{true}}{os.linesep}"
|
str(upload_component) == f"<ReactDropzone multiple={{true}}{os.linesep}"
|
||||||
"onDrop={e => File(e)}>{({getRootProps, getInputProps}) => (<Box "
|
"onDrop={e => File(e)}>{({getRootProps, getInputProps}) => (<Box "
|
||||||
'sx={{"border": "1px dotted black"}}{...getRootProps()}><Input '
|
'sx={{"border": "1px dotted black"}}{...getRootProps()}><Input '
|
||||||
f"type={{`file`}}{{...getInputProps()}}/>{os.linesep}"
|
f'type="file"{{...getInputProps()}}/>{os.linesep}'
|
||||||
f"<Button>{{`select file`}}</Button>{os.linesep}"
|
f"<Button>{{`select file`}}</Button>{os.linesep}"
|
||||||
"<Text>{`Drag and drop files here or click to select "
|
"<Text>{`Drag and drop files here or click to select "
|
||||||
"files`}</Text></Box>)}</ReactDropzone>"
|
"files`}</Text></Box>)}</ReactDropzone>"
|
||||||
@ -72,7 +72,7 @@ def test_upload_component_with_props_render(upload_component_with_props):
|
|||||||
f"noDrag={{true}}{os.linesep}"
|
f"noDrag={{true}}{os.linesep}"
|
||||||
"onDrop={e => File(e)}>{({getRootProps, getInputProps}) => (<Box "
|
"onDrop={e => File(e)}>{({getRootProps, getInputProps}) => (<Box "
|
||||||
'sx={{"border": "1px dotted black"}}{...getRootProps()}><Input '
|
'sx={{"border": "1px dotted black"}}{...getRootProps()}><Input '
|
||||||
f"type={{`file`}}{{...getInputProps()}}/>{os.linesep}"
|
f'type="file"{{...getInputProps()}}/>{os.linesep}'
|
||||||
f"<Button>{{`select file`}}</Button>{os.linesep}"
|
f"<Button>{{`select file`}}</Button>{os.linesep}"
|
||||||
"<Text>{`Drag and drop files here or click to select "
|
"<Text>{`Drag and drop files here or click to select "
|
||||||
"files`}</Text></Box>)}</ReactDropzone>"
|
"files`}</Text></Box>)}</ReactDropzone>"
|
||||||
|
@ -6,7 +6,7 @@ import pynecone as pc
|
|||||||
from pynecone.components.component import Component, CustomComponent, custom_component
|
from pynecone.components.component import Component, CustomComponent, custom_component
|
||||||
from pynecone.components.layout.box import Box
|
from pynecone.components.layout.box import Box
|
||||||
from pynecone.event import EVENT_ARG, EVENT_TRIGGERS, EventHandler
|
from pynecone.event import EVENT_ARG, EVENT_TRIGGERS, EventHandler
|
||||||
from pynecone.state import DefaultState, State
|
from pynecone.state import State
|
||||||
from pynecone.style import Style
|
from pynecone.style import Style
|
||||||
from pynecone.utils import imports
|
from pynecone.utils import imports
|
||||||
from pynecone.var import Var
|
from pynecone.var import Var
|
||||||
@ -434,27 +434,3 @@ def test_get_hooks_nested2(component3, component4):
|
|||||||
).get_hooks()
|
).get_hooks()
|
||||||
== exp_hooks
|
== exp_hooks
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_set_state(component1, component2, component3):
|
|
||||||
"""Test that setting the state of a component works.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
component1: test component.
|
|
||||||
component2: another component.
|
|
||||||
component3: component with hooks defined.
|
|
||||||
"""
|
|
||||||
c2 = component2.create()
|
|
||||||
c3 = component3.create()
|
|
||||||
c1 = component1.create(c2, c3)
|
|
||||||
|
|
||||||
# State should be None by default.
|
|
||||||
assert c1.state is None
|
|
||||||
assert c2.state is None
|
|
||||||
assert c3.state is None
|
|
||||||
|
|
||||||
# Setting the parent state should set the child state.
|
|
||||||
c1.set_state(DefaultState)
|
|
||||||
assert c1.state == DefaultState
|
|
||||||
assert c2.state == DefaultState
|
|
||||||
assert c3.state == DefaultState
|
|
||||||
|
Loading…
Reference in New Issue
Block a user