实践reflex:以AI Chat APP为例

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.mzph.cn/web/52244.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

新一代交互模式:LUICUIVUI

随着技术的发展,特别是人工智能和机器学习的进步,交互方式也在不断演变。以下是一些新概念,它们描述了当下和未来可能的交互方式: Conversational UI (CUI): 以对话为基础的用户界面,用户通过自然语言与系统…

计算机网络(四) —— 简单Tcp网络程序

目录 一,服务器初始化 1.0 部分文件代码 1.1 关于Tcp协议 1.2 创建和绑定套接字 1.3 监听 二,服务器启动 2.1 获取连接 2.2 提供服务 2.3 客户端启动源文件 Main.cc 二,客户端编写 2.1 关于Tcp客户端 2.2 客户端代码 2.3 效果…

网络学习-eNSP配置多交换机VLAN

实验环境 通过华为eNSP软件实现 1.两台S3700交换机 2.四台电脑PC1:192.168.0.1PC2:192.168.0.2PC3:192.168.0.3PC4:192.168.0.4PC11:192.168.0.11PC22:192.168.0.22PC33:192.168.0.33PC44&…

win12R2安装.NET Framework 3.5

一丶安装原因 因此插件的缺失, 有些软件或系统不支持安装. 二丶安装步骤 1丶下载.NET Framework 3.5 点击插件下载, 提取码: 1995, 下载完成之后解压到想要安装的位置上. 2丶打开 服务器管理器 3丶点击: 管理 -> 添加角色和功能 4丶点击下一步到服务器角色, 选择web服…

IOS17.0安装巨魔:TrollRestore巨魔发布

👻 TrollRestore 17.0 巨魔发布 15.0 - 16.7 RC(20H18)和17.0。 官网:https://trollrestore.com/ 下载:https://pan.metanetdisk.com/IOS/%E5%B7%A8%E9%AD%94%E7%8E%A9%E5%AE%B6/TrollRestore.com 使用:ht…

【技巧】Excel检查单元格的值是否在另一列中

转载请注明出处:小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你,欢迎[点赞、收藏、关注]哦~ 用到的excel函数 IF(ISNUMBER(MATCH(H2, I2:I10, 0)), H2, "") 注意改上面的“H2、I2、I10”! 函数效果 函数解释 检查单元格 …

Keysight U8031A DC power supply

Keysight U8031A DC power supply 文章目录 Keysight U8031A DC power supply前言电容充电⽰意图一、恒定电压操作二、恒定电流操作三、5v操作四、跟踪模式操作五、存储器操作六、对过电压保护编程七、对过电流保护编程八、锁键操作 前言 U8031A Power Supply 是一款具备前面板…

域取证的日志分析

目录 介绍步骤横向移动行为分析 介绍 1、evtx文件是微软从 Windows NT 6.0(Windows Vista 和 Server 2008) 开始采用的一种全新的日志文件格式。在此之前的格式是 evt 。evtx由Windows事件查看器创建,包含Windows记录的事件列表,以专有的二进制XML格式保…

关于武汉高芯coin417G2红外机芯的二次开发

文章目录 前言一、外观和机芯参数二、SDK的使用1、打开相机2、回调函数中获取全局温度和图像3、关闭相机 前言 最近工作中接触了一款基于武汉高芯科技有限公司开发的红外模组,即coin417g2(测温型)9.1mm镜头.使用此模组,开发了一套红外热成像检测桌面应用程序.下面简单记录下该…

【2024高教社杯全国大学生数学建模竞赛】B题模型建立求解

目录 1问题重述1.1问题背景1.2研究意义1.3具体问题 2总体分析3模型假设4符号说明(等四问全部更新完再写)5模型的建立与求解5.1问题一模型的建立与求解5.1.1问题的具体分析5.1.2模型的准备 目前B题第一问的详细求解过程以及对应论文部分已经完成&#xff…

RISC-V (九)抢占式多任务

主要的思想:借用定时器中断实现。设置定时器寄存器,系统自动触发定时器中断时会跳到trap handler这个函数里。借用这个函数做上下文的切换,从而实现了抢占式多任务。 定时器中断:跳到trap handler函数,同时系统自动将…

软考基础知识之计算机网络

目录 前言 网络架构与协议 网络互联模型 1、OSI/RM 各层的功能 2、TCP/IP 结构模型 常见的网络协议 1、应用层协议 2、传输层协议 3、网络层协议 IPv6 前言 从古代的驿站、 八百里快马, 到近代的电报、 电话, 人类对于通信的追求从未间断&…

心觉:你为什么没有更多的钱

很多人希望自己可以赚更多的钱 但是他的内心又很讨厌钱,他自己并不知道 一边希望自己赚更多钱,一边在骨子里觉得“金钱是万恶之源” 这是一种神经质的错乱 这种现象在什么情况下会表现得比较明显呢? 某位高官因为贪污受贿落马了&#xf…

风控系统之指标回溯,历史数据重跑

个人博客:无奈何杨(wnhyang) 个人语雀:wnhyang 共享语雀:在线知识共享 Github:wnhyang - Overview 回顾 默认你已经看过之前那篇风控系统指标计算/特征提取分析与实现01,Redis、Zset、模版方…

精通Java微服务

第1章 微服务是在面向服务架构SOA的基础上进一步发展而来的,它比SOA做得更加彻底,其单体服务被更加彻底地划分,最大限度地实现了服务的单一职责。 1.1.2互联网 即计算机网络,连接了世界上数以万计的计算机设备(可联…

15、Django Admin添加自定义字段功能

修改模型类HeroAdmin admin.register(Hero) class HeroAdmin(admin.ModelAdmin):change_list_template "entities/heroes_changelist.html"... # 此处原代码不动,只增加此前后代码def get_urls(self):urls super().get_urls()my_urls [path(immort…

最新版 | SpringBoot3如何自定义starter(面试常考)

文章目录 一、自定义starter介绍二、自定义Starter的好处及优势三、自定义starter应用场景四、自定义starter1、创建autoconfigure的maven工程2、创建starter的maven工程3、在autoconfigure的pom文件中引入MyBatis的所需依赖4、编写自动配置类MyBatisAutoConfiguration5、编写i…

pdf文件编辑器有哪些?分享适合新手用的5个PDF编辑器(解锁教程)

pdf是一种通用文件格式,也是一种夸操作系统平台的文件格式。 好用的PDF文件编辑器可以让您更改和添加文本、编辑图像、添加图形、签署签名、填写表单数据等。下面整理了关于pdf文件编辑方法介绍,以及一些好用的pdf编辑器,有需要的可以了解下…

C# 通过拖控件移动窗体

目录 引言一、通过控件事件移动窗体1、创建窗体界面2、添加控件事件3、添加代码 二、通过windowsAPI移动窗体1、 构建窗体和添加事件2、代码展示 引言 在C#Form窗体设计中,如果我们不需要使用默认边框设计自己个性化的窗体(FromBorderStylenone时&#…

LEAN 类型理论之注解(Annotations of LEAN Type Theory)-- 商类型(Quotient Type)

商类型(Quotient Type),也称划分类型,通过给定义一个定义在某一类型 α 上的关系R:α → α→ ℙ,将类型α 中,满足关系R的元素摘出来,组成该商类型(Quotient&#xff09…