Created
November 11, 2022 23:59
-
-
Save kbrgl/2dcb13b29e70e31757f2ab73decf997b to your computer and use it in GitHub Desktop.
Repro for state bugs
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
"""Welcome to Pynecone! This file outlines the steps to create a basic app.""" | |
# Import pynecone. | |
from datetime import datetime | |
import pcconfig | |
import pynecone as pc | |
from pynecone.base import Base | |
docs_url = "https://pynecone.io/docs/getting-started/introduction" | |
title = "Welcome to Pynecone!" | |
filename = f"{pcconfig.APP_NAME}/{pcconfig.APP_NAME}.py" | |
class Message(Base): | |
text: str | |
created_at: float | |
class State(pc.State): | |
"""The app state.""" | |
text: str = "" | |
message_store: list[Message] = [ | |
Message(text="Hello!", created_at=datetime.now().timestamp()) | |
] | |
@pc.var | |
def output(self) -> str: | |
return self.text.upper() | |
@pc.var | |
def messages(self) -> list[str]: | |
return [m.text for m in self.message_store] | |
def post(self): | |
self.message_store += [ | |
Message(text=self.output, created_at=datetime.now().timestamp()) | |
] | |
# Define views. | |
def header(): | |
"""Basic instructions to get started.""" | |
return pc.vstack( | |
pc.box("Translator", font_size="2rem"), | |
pc.text("Enter text to translate"), | |
spacing="1rem", | |
) | |
def index(): | |
"""The main view.""" | |
return pc.container( | |
pc.vstack( | |
header(), | |
pc.input( | |
placeholder="Text to translate", | |
on_change=State.set_text, | |
), | |
pc.input( | |
placeholder="Output will appear here", | |
value=State.output, | |
), | |
pc.button("Post", on_click=State.post), | |
pc.text(State.messages), | |
spacing="1rem", | |
), | |
padding_y="5rem", | |
text_align="center", | |
) | |
# Add state and page to the app. | |
app = pc.App(state=State) | |
app.add_page(index, title=title) | |
app.compile() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment