From e6f528565f999b4597ea39a6b2c80e451f9c2ea6 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 7 Sep 2023 14:05:47 -0700 Subject: [PATCH 1/3] chatroom: use app.event_namespace The event endpoint changed in 0.2.7, so use this more reliable method of getting a handle to the event_namespace. --- chatroom/chatroom/chatroom.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chatroom/chatroom/chatroom.py b/chatroom/chatroom/chatroom.py index 26ea597d..1e3701e6 100644 --- a/chatroom/chatroom/chatroom.py +++ b/chatroom/chatroom/chatroom.py @@ -91,7 +91,6 @@ def index() -> rx.Component: async def broadcast_event(name: str, payload: t.Dict[str, t.Any] = {}) -> None: """Simulate frontend event with given name and payload from all clients.""" - event_ns = app.sio.namespace_handlers["/event"] responses = [] for state in app.state_manager.states.values(): async for update in state._process( @@ -104,7 +103,7 @@ async def broadcast_event(name: str, payload: t.Dict[str, t.Any] = {}) -> None: ): # Emit the event. responses.append( - event_ns.emit( + app.event_namespace.emit( str(rx.constants.SocketEvent.EVENT), update.json(), to=state.get_sid() ), ) From ee8f7b443d3fff9b67d9a393d4871fa0041a9e34 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 7 Sep 2023 14:08:57 -0700 Subject: [PATCH 2/3] sales: cleanups for 0.2.5+ Move Customer model to separate file Fix chained event handler call: use State.call_openai Enable AdminDash as an example --- sales/sales/models.py | 13 +++++++++++++ sales/sales/sales.py | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 sales/sales/models.py diff --git a/sales/sales/models.py b/sales/sales/models.py new file mode 100644 index 00000000..2b383a50 --- /dev/null +++ b/sales/sales/models.py @@ -0,0 +1,13 @@ +import reflex as rx + + +class Customer(rx.Model, table=True): + """The customer model.""" + + customer_name: str + email: str + age: int + gender: str + location: str + job: str + salary: int diff --git a/sales/sales/sales.py b/sales/sales/sales.py index 9af46b27..cf83c2b4 100644 --- a/sales/sales/sales.py +++ b/sales/sales/sales.py @@ -106,7 +106,7 @@ def generate_email( self.generate_email_data["salary"] = salary self.text_area_disabled = True self.gen_response = True - return self.call_openai + return State.call_openai @@ -308,7 +308,7 @@ def index(): # Add state and page to the app. -app = rx.App(state=State) +app = rx.App(state=State, admin_dash=rx.AdminDash(models=[Customer])) app.add_page(index) app.add_page(add_customer, "/onboarding") app.compile() From 76c9dc6210504dcad7b2d633b76ddbfbfc307d70 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 7 Sep 2023 14:09:52 -0700 Subject: [PATCH 3/3] openai apps: Accept OPENAI_API_KEY from environment This is the standard env var for openai key, and the examples should NOT be encouraging users to hardcode secrets in their python source code. --- dalle/dalle/dalle.py | 6 ++++-- gpt/gpt/gpt.py | 9 ++++++--- sales/sales/sales.py | 20 ++++++-------------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/dalle/dalle/dalle.py b/dalle/dalle/dalle.py index 420adb7a..a186ba47 100644 --- a/dalle/dalle/dalle.py +++ b/dalle/dalle/dalle.py @@ -1,8 +1,10 @@ """Welcome to Pynecone! This file outlines the steps to create a basic app.""" -import reflex as rx +import os + import openai +import reflex as rx -openai.api_key = "YOUR_API_KEY" +openai.api_key = os.environ["OPENAI_API_KEY"] class State(rx.State): diff --git a/gpt/gpt/gpt.py b/gpt/gpt/gpt.py index 73f82d66..12c9c604 100644 --- a/gpt/gpt/gpt.py +++ b/gpt/gpt/gpt.py @@ -1,10 +1,13 @@ """Welcome to Reflex! This app is a demonstration of OpenAI's GPT.""" +import datetime +import os + +import openai import reflex as rx + from .helpers import navbar -import openai -import datetime -openai.api_key = "YOUR_API_KEY" +openai.api_key = os.environ["OPENAI_API_KEY"] MAX_QUESTIONS = 10 diff --git a/sales/sales/sales.py b/sales/sales/sales.py index cf83c2b4..45acdd3f 100644 --- a/sales/sales/sales.py +++ b/sales/sales/sales.py @@ -1,20 +1,12 @@ -import reflex as rx -import openai - -openai.api_key = "YOUR_OPENAI_API_KEY" -products = {'T-shirt': { 'description': 'A plain white t-shirt made of 100% cotton.', 'price': 10.99 }, 'Jeans': { 'description': 'A pair of blue denim jeans with a straight leg fit.', 'price': 24.99 }, 'Hoodie': { 'description': 'A black hoodie made of a cotton and polyester blend.', 'price': 34.99 }, 'Cardigan': { 'description': 'A grey cardigan with a V-neck and long sleeves.', 'price': 36.99 }, 'Joggers': { 'description': 'A pair of black joggers made of a cotton and polyester blend.', 'price': 44.99 }, 'Dress': { 'description': 'A black dress made of 100% polyester.', 'price': 49.99 }, 'Jacket': { 'description': 'A navy blue jacket made of 100% cotton.', 'price': 55.99 }, 'Skirt': { 'description': 'A brown skirt made of a cotton and polyester blend.', 'price': 29.99 }, 'Shorts': { 'description': 'A pair of black shorts made of a cotton and polyester blend.', 'price': 19.99 }, 'Sweater': { 'description': 'A white sweater with a crew neck and long sleeves.', 'price': 39.99}} +import os +import openai +import reflex as rx -class Customer(rx.Model, table=True): - """The customer model.""" +from .models import Customer - customer_name: str - email: str - age: int - gender: str - location: str - job: str - salary: int +openai.api_key = os.environ["OPENAI_API_KEY"] +products = {'T-shirt': { 'description': 'A plain white t-shirt made of 100% cotton.', 'price': 10.99 }, 'Jeans': { 'description': 'A pair of blue denim jeans with a straight leg fit.', 'price': 24.99 }, 'Hoodie': { 'description': 'A black hoodie made of a cotton and polyester blend.', 'price': 34.99 }, 'Cardigan': { 'description': 'A grey cardigan with a V-neck and long sleeves.', 'price': 36.99 }, 'Joggers': { 'description': 'A pair of black joggers made of a cotton and polyester blend.', 'price': 44.99 }, 'Dress': { 'description': 'A black dress made of 100% polyester.', 'price': 49.99 }, 'Jacket': { 'description': 'A navy blue jacket made of 100% cotton.', 'price': 55.99 }, 'Skirt': { 'description': 'A brown skirt made of a cotton and polyester blend.', 'price': 29.99 }, 'Shorts': { 'description': 'A pair of black shorts made of a cotton and polyester blend.', 'price': 19.99 }, 'Sweater': { 'description': 'A white sweater with a crew neck and long sleeves.', 'price': 39.99}} class State(rx.State):