Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.

Commit 082b2b1

Browse files
committed
get reflex web to source its files from reflex
1 parent 3c1209e commit 082b2b1

9 files changed

Lines changed: 1909 additions & 270 deletions

File tree

docs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Reflex documentation."""

docs/getting_started/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Getting started docs."""
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Common styles for questions and answers."""
2+
3+
import reflex as rx
4+
5+
shadow = "rgba(0, 0, 0, 0.15) 0px 2px 8px"
6+
chat_margin = "20%"
7+
message_style = {
8+
"padding": "1em",
9+
"border_radius": "5px",
10+
"margin_y": "0.5em",
11+
"box_shadow": shadow,
12+
"max_width": "30em",
13+
"display": "inline-block",
14+
}
15+
16+
# Set specific styles for questions and answers.
17+
question_style = message_style | {
18+
"background_color": rx.color("gray", 4),
19+
"margin_left": chat_margin,
20+
}
21+
answer_style = message_style | {
22+
"background_color": rx.color("accent", 8),
23+
"margin_right": chat_margin,
24+
}
25+
26+
# Styles for the action bar.
27+
input_style = {"border_width": "1px", "box_shadow": shadow, "width": "350px"}
28+
button_style = {"background_color": rx.color("accent", 10), "box_shadow": shadow}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""Utility classes for the chat app tutorial."""
2+
3+
from __future__ import annotations
4+
5+
import os
6+
7+
import openai # pyright: ignore[reportMissingImports]
8+
9+
import reflex as rx
10+
11+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
12+
13+
14+
class ChatappState(rx.State):
15+
"""State for the chat app tutorial."""
16+
17+
# The current question being asked.
18+
question: str
19+
20+
# Keep track of the chat history as a list of (question, answer) tuples.
21+
chat_history: list[tuple[str, str]]
22+
23+
def set_question(self, q: str):
24+
"""Set the current question."""
25+
self.question = q
26+
27+
def set_question1(self, q: str):
28+
"""Set the current question (variant 1)."""
29+
self.question = q
30+
31+
def set_question2(self, q: str):
32+
"""Set the current question (variant 2)."""
33+
self.question = q
34+
35+
def set_question3(self, q: str):
36+
"""Set the current question (variant 3)."""
37+
self.question = q
38+
39+
def answer(self) -> None:
40+
"""Answer the question with a static response."""
41+
# Our chatbot is not very smart right now...
42+
answer = "I don't know!"
43+
self.chat_history.append((self.question, answer))
44+
45+
def answer2(self) -> None:
46+
"""Answer the question and clear the input."""
47+
# Our chatbot is not very smart right now...
48+
answer = "I don't know!"
49+
self.chat_history.append((self.question, answer))
50+
# Clear the question input.
51+
self.question = ""
52+
53+
async def answer3(self):
54+
"""Answer with a streaming static response."""
55+
import asyncio
56+
57+
# Our chatbot is not very smart right now...
58+
answer = "I don't know!"
59+
self.chat_history.append((self.question, ""))
60+
61+
# Clear the question input.
62+
self.question = ""
63+
# Yield here to clear the frontend input before continuing.
64+
yield
65+
66+
for i in range(len(answer)):
67+
await asyncio.sleep(0.1)
68+
self.chat_history[-1] = (self.chat_history[-1][0], answer[: i + 1])
69+
yield
70+
71+
async def answer4(self):
72+
"""Answer using the OpenAI API with streaming."""
73+
# Our chatbot has some brains now!
74+
client = openai.AsyncOpenAI(api_key=OPENAI_API_KEY)
75+
session = await client.chat.completions.create(
76+
model="gpt-4o-mini",
77+
messages=[{"role": "user", "content": self.question}],
78+
stop=None,
79+
temperature=0.7,
80+
stream=True,
81+
)
82+
83+
# Add to the answer as the chatbot responds.
84+
answer = ""
85+
self.chat_history.append((self.question, answer))
86+
87+
# Clear the question input.
88+
self.question = ""
89+
# Yield here to clear the frontend input before continuing.
90+
yield
91+
92+
async for item in session:
93+
if hasattr(item.choices[0].delta, "content"):
94+
if item.choices[0].delta.content is None:
95+
# presence of 'None' indicates the end of the response
96+
break
97+
answer += item.choices[0].delta.content
98+
self.chat_history[-1] = (self.chat_history[-1][0], answer)
99+
yield

0 commit comments

Comments
 (0)