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

Move toy examples to "playground" app #292

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
move lorem-stream to playground and refactor
masenf committed Jan 14, 2025
commit 4cbbe434c63e554784258f89c796b098dab8b8a6
4 changes: 0 additions & 4 deletions lorem-stream/.gitignore

This file was deleted.

35 changes: 0 additions & 35 deletions lorem-stream/README.md

This file was deleted.

Binary file removed lorem-stream/assets/favicon.ico
Binary file not shown.
Empty file.
3 changes: 0 additions & 3 deletions lorem-stream/requirements.txt

This file was deleted.

5 changes: 0 additions & 5 deletions lorem-stream/rxconfig.py

This file was deleted.

3 changes: 2 additions & 1 deletion playground/playground/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import clock, json_tree
from . import clock, json_tree, lorem_stream

__all__ = [
"clock",
"json_tree",
"lorem_stream",
]
8 changes: 8 additions & 0 deletions playground/playground/lorem_stream/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ..common import demo
from .lorem_stream import example

demo(
route="/lorem_stream",
title="Lorem Streaming Background Tasks",
description="Demonstrates how to use background tasks to stream text concurrently.",
)(example)
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import asyncio
import random

from lorem_text import lorem

import reflex as rx
import reflex_chakra as rc

from lorem_text import lorem

ITERATIONS_RANGE = (7, 12)

@@ -18,11 +15,11 @@ class LoremState(rx.State):

_next_task_id: int = 0

@rx.var
@rx.var(cache=True)
def task_ids(self) -> list[int]:
return list(reversed(self.text))

@rx.background
@rx.event(background=True)
async def stream_text(self, task_id: int = -1):
if task_id < 0:
async with self:
@@ -45,6 +42,7 @@ async def stream_text(self, task_id: int = -1):
async with self:
self.running.pop(task_id, None)

@rx.event
def toggle_running(self, task_id: int):
if self.progress.get(task_id, 0) >= self.end_at.get(task_id, 0):
self.progress[task_id] = 0
@@ -55,6 +53,7 @@ def toggle_running(self, task_id: int):
else:
return LoremState.stream_text(task_id)

@rx.event
def kill(self, task_id: int):
self.running.pop(task_id, None)
self.text.pop(task_id, None)
@@ -63,31 +62,41 @@ def kill(self, task_id: int):
def render_task(task_id: int) -> rx.Component:
return rx.vstack(
rx.hstack(
rc.circular_progress(
rc.circular_progress_label(task_id),
value=LoremState.progress[task_id],
max_=LoremState.end_at[task_id],
is_indeterminate=LoremState.progress[task_id] < 1,
),
rx.badge("Task ", task_id),
rx.button(
rx.cond(
LoremState.progress[task_id] < LoremState.end_at[task_id], "⏯️", "🔄"
),
on_click=LoremState.toggle_running(task_id),
variant="outline",
),
rx.button("❌", on_click=LoremState.kill(task_id)),
rx.button(
"❌",
on_click=LoremState.kill(task_id),
variant="outline",
),
),
rx.progress(
value=LoremState.progress[task_id],
max=LoremState.end_at[task_id],
min_height="10px",
max_height="10px",
),
rx.text(LoremState.text[task_id], overflow_y="scroll"),
rx.spacer(),
width=["180px", "190px", "210px", "240px", "300px"],
height="300px",
padding="10px",
)


@rx.page(title="Lorem Streaming Background Tasks")
def index() -> rx.Component:
def example() -> rx.Component:
return rx.vstack(
rx.button("➕ New Task", on_click=LoremState.stream_text(-1)),
rx.button(
"➕ New Task", # noqa: RUF001
on_click=LoremState.stream_text(-1),
variant="surface",
),
rx.flex(
rx.foreach(LoremState.task_ids, render_task),
flex_wrap="wrap",
@@ -96,6 +105,3 @@ def index() -> rx.Component:
align="center",
padding_top="20px",
)


app = rx.App()
4 changes: 3 additions & 1 deletion playground/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
reflex>=0.6.8

pytz # for "clock"
pytz # for "clock"

lorem_text>=2.1 # for "lorem_stream"