实践reflex:以AI Chat APP为例

devtools/2024/9/22 21:31:38/

reflex demo
手册:Intro

以AI Chat APP为例

Interactive Tutorial: AI Chat App

This tutorial will walk you through building an AI chat app with Reflex. This app is fairly complex, but don't worry - we'll break it down into small steps.

You can find the full source code for this app here.

What You'll Learn

In this tutorial you'll learn how to:

  1. Install reflex and set up your development environment.
  2. Create components to define and style your UI.
  3. Use state to add interactivity to your app.
  4. Deploy your app to share with others.

安装reflex并初始化项目

chatapp $ pip install reflex
chatapp $ reflex init
────────────────────────────────── Initializing chatapp ───────────────────────────────────
Success: Initialized chatapp
chatapp $ ls
assets          chatapp         rxconfig.py     venv

使用reflex init初始化,选择3 ,将其初始化成chatapp项目

启动项目

chatapp $ reflex run
─────────────────────────────────── Starting Reflex App ───────────────────────────────────
Compiling:  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 1/1 0:00:00
─────────────────────────────────────── App Running ───────────────────────────────────────
App running at: http://localhost:3000

前端

显示问答

# chatapp.pyimport reflex as rxdef index() -> rx.Component:return rx.container(rx.box("What is Reflex?",# The user's question is on the right.text_align="right",),rx.box("A way to build web apps in pure Python!",# The answer is on the left.text_align="left",),)# Add state and page to the app.
app = rx.App()
app.add_page(index)

重用部件

def qa(question: str, answer: str) -> rx.Component:return rx.box(rx.box(question, text_align="right"),rx.box(answer, text_align="left"),margin_y="1em",)def chat() -> rx.Component:qa_pairs = [("What is Reflex?","A way to build web apps in pure Python!",),("What can I make with it?","Anything from a simple website to a complex web app!",),]return rx.box(*[qa(question, answer)for question, answer in qa_pairs])def index() -> rx.Component:return rx.container(chat())

输入交互对话框

def action_bar() -> rx.Component:return rx.hstack(rx.input(placeholder="Ask a question"),rx.button("Ask"),)def index() -> rx.Component:return rx.container(chat(),action_bar(),)

样式

# style.py
import reflex as rx# Common styles for questions and answers.
shadow = "rgba(0, 0, 0, 0.15) 0px 2px 8px"
chat_margin = "20%"
message_style = dict(padding="1em",border_radius="5px",margin_y="0.5em",box_shadow=shadow,max_width="30em",display="inline-block",
)# Set specific styles for questions and answers.
question_style = message_style | dict(margin_left=chat_margin,background_color=rx.color("gray", 4),
)
answer_style = message_style | dict(margin_right=chat_margin,background_color=rx.color("accent", 8),
)# Styles for the action bar.
input_style = dict(border_width="1px", padding="1em", box_shadow=shadow
)
button_style = dict(background_color=rx.color("accent", 10),box_shadow=shadow,
)

最终代码

# chatapp.py
import reflex as rxfrom chatapp import styledef qa(question: str, answer: str) -> rx.Component:return rx.box(rx.box(rx.text(question, style=style.question_style),text_align="right",),rx.box(rx.text(answer, style=style.answer_style),text_align="left",),margin_y="1em",)def chat() -> rx.Component:qa_pairs = [("What is Reflex?","A way to build web apps in pure Python!",),("What can I make with it?","Anything from a simple website to a complex web app!",),]return rx.box(*[qa(question, answer)for question, answer in qa_pairs])def action_bar() -> rx.Component:return rx.hstack(rx.input(placeholder="Ask a question",style=style.input_style,),rx.button("Ask", style=style.button_style),)def index() -> rx.Component:return rx.center(rx.vstack(chat(),action_bar(),align="center",))app = rx.App()
app.add_page(index)

后端

代码

# chatapp.py
import reflex as rxfrom chatapp import styledef qa(question: str, answer: str) -> rx.Component:return rx.box(rx.box(rx.text(question, style=style.question_style),text_align="right",),rx.box(rx.text(answer, style=style.answer_style),text_align="left",),margin_y="1em",)def chat() -> rx.Component:qa_pairs = [("What is Reflex?","A way to build web apps in pure Python!",),("What can I make with it?","Anything from a simple website to a complex web app!",),]return rx.box(*[qa(question, answer)for question, answer in qa_pairs])def action_bar() -> rx.Component:return rx.hstack(rx.input(placeholder="Ask a question",style=style.input_style,),rx.button("Ask", style=style.button_style),)def index() -> rx.Component:return rx.center(rx.vstack(chat(),action_bar(),align="center",))app = rx.App()
app.add_page(index)

最终代码

前端

# chatapp.py
import reflex as rxfrom chatapp import style
from chatapp.state import TutorialStatedef qa(question: str, answer: str) -> rx.Component:return rx.box(rx.box(rx.text(question, text_align="right"),style=style.question_style,),rx.box(rx.text(answer, text_align="left"),style=style.answer_style,),)def chat() -> rx.Component:return rx.box(rx.foreach(TutorialState.chat_history,lambda messages: qa(messages[0], messages[1]),))def action_bar() -> rx.Component:return rx.hstack(rx.input(placeholder="Ask a question",value=TutorialState.question,on_change=TutorialState.set_question,style=style.input_style,),rx.button("Ask",on_click=TutorialState.answer,style=style.button_style,),)def index() -> rx.Component:return rx.center(rx.vstack(chat(),action_bar(),align="center",))app = rx.App()
app.add_page(index)

后端

# chatapp.py
import reflex as rxfrom chatapp import style
from chatapp.state import TutorialStatedef qa(question: str, answer: str) -> rx.Component:return rx.box(rx.box(rx.text(question, text_align="right"),style=style.question_style,),rx.box(rx.text(answer, text_align="left"),style=style.answer_style,),)def chat() -> rx.Component:return rx.box(rx.foreach(TutorialState.chat_history,lambda messages: qa(messages[0], messages[1]),))def action_bar() -> rx.Component:return rx.hstack(rx.input(placeholder="Ask a question",value=TutorialState.question,on_change=TutorialState.set_question,style=style.input_style,),rx.button("Ask",on_click=TutorialState.answer,style=style.button_style,),)def index() -> rx.Component:return rx.center(rx.vstack(chat(),action_bar(),align="center",))app = rx.App()
app.add_page(index)

函数处理

# style.py
import reflex as rx# Common styles for questions and answers.
shadow = "rgba(0, 0, 0, 0.15) 0px 2px 8px"
chat_margin = "20%"
message_style = dict(padding="1em",border_radius="5px",margin_y="0.5em",box_shadow=shadow,max_width="30em",display="inline-block",
)# Set specific styles for questions and answers.
question_style = message_style | dict(background_color=rx.color("gray", 4),margin_left=chat_margin,
)
answer_style = message_style | dict(background_color=rx.color("accent", 8),margin_right=chat_margin,
)# Styles for the action bar.
input_style = dict(border_width="1px",padding="1em",box_shadow=shadow,width="350px",
)
button_style = dict(background_color=rx.color("accent", 10),box_shadow=shadow,
)


http://www.ppmy.cn/devtools/108757.html

相关文章

计算机网络:URL构成

计算机网络&#xff1a;URL构成 一、本文内容与前置知识点1. 本文内容 二、URL基本构成1. 协议2. 主机3. 端口4. 路径 四、参考文献 一、本文内容与前置知识点 1. 本文内容 URL构成说明。 二、URL基本构成 参考《计算机网络》6.4.2统一资源配置符URL p723 <协议>&…

C#/WinForm演示最小二乘法拟合一次函数

一、什么是最小二乘法 最小二乘法&#xff08;Least Squares Method&#xff09;是一种数学优化技术&#xff0c;常用于拟合数据和估计参数。它的主要目标是找到一个函数&#xff0c;使其预测值与观测值之间的残差平方和最小化。 在最小二乘法中&#xff0c;通常考虑一个具有n…

react lazy加载资源找不到的问题

在 Umi 4 中&#xff0c;默认按页拆包进行优化&#xff0c;实现每个页面只需加载最少的 js 资源&#xff0c;这会产生很多异步 js 分包。通常我们会开启 hash: true 构建&#xff0c;将 js / css 等资源做长期缓存&#xff0c;而 html 不缓存。 然而&#xff0c;在版本发布时&…

探寻 IP 代理地址繁多之因

在当今的网络天地里&#xff0c;IP 代理服务随处可见&#xff0c;且令人称奇的是&#xff0c;它们常常手握海量的 IP 地址可供挑选。那么&#xff0c;究竟是什么原因使得 IP 代理拥有如此众多的地址呢&#xff1f;现在&#xff0c;就让我们一同深入探究这个神秘现象背后的缘由。…

数据可视化的必要前提:数据清洗

随着大数据技术的迅猛发展&#xff0c;企业正处于数字化转型的关键时期&#xff0c;这涉及到将传统的业务流程和服务升级为依托于数字技术的新形态。为了提升行业竞争力&#xff0c;企业必须构建起高效的数据化系统&#xff0c;以实现对市场变化的敏捷响应。 在此过程中&#…

iOS——frame和bounds的区别

把frame理解为占用区域&#xff0c;把bounds理解为边界。View在旋转过程中&#xff0c;其实自己的坐标系统并没有发生改变&#xff0c;bounds中的origin只能通过setBounds方法修改。 frame 定义了视图在其父视图坐标系统中的位置和大小。其坐标系是相对于俯视图的坐标系。 bou…

贝锐蒲公英远程视频监控方案:4G入网无需公网IP,跨品牌统一管理

在部署视频监控并实现集中监看时&#xff0c;常常会遇到各种挑战。比如&#xff1a;部分监控点位布线困难、无法接入有线宽带&#xff0c;或是没有固定公网IP&#xff0c;难以实现远程集中监看&#xff1b;已有网络质量差&#xff0c;传输延迟大、丢包率高&#xff0c;远程实时…

linux 安装redis

1. 更新系统和安装依赖 sudo apt update sudo apt install build-essential tcl2. 下载 Redis 源码(没有opt文件夹&#xff0c;则先创建opt文件夹) cd /opt wget http://download.redis.io/releases/redis-6.2.6.tar.gz3. 解压和编译 Redis 解压下载的文件&#xff0c;并进入…