实践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,一经查实,立即删除!

相关文章

el-table 封装表格(完整代码-实时更新)

最新更新时间&#xff1a; 2024年9月6号 1. 添加行内编辑、表头搜索 <template><!-- 简单表格、多层表头、页码、没有合并列行 --><div class"maintenPublictable"element-loading-background"rgba(255,255,255,0.5)"><!--cell-sty…

新一代交互模式:LUICUIVUI

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

mysql数据库8.0小版本原地升级

mysql数据库8.0小版本原地升级 准备工作升级工作停库使用新版本软件启动数据库更新环境变量重启数据库 升级日志 OS release: CentOS 7.9升级前DB version: MySQL 8.0.30数据库升级安装包&#xff1a;mysql-8.0.36-linux-glibc2.12-x86_64.tar.xzMySQL Shell安装包&#xff1a;…

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

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

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

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

win12R2安装.NET Framework 3.5

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

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

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

Java DAO 层:数据访问对象模式解析与实践

Java DAO 层&#xff1a;数据访问对象模式解析与实践 在软件开发中&#xff0c;数据访问层&#xff08;Data Access Object&#xff0c;简称DAO&#xff09;是一个至关重要的组件&#xff0c;它负责封装对数据源的所有访问。DAO 层的设计和实现对于提高应用程序的可维护性、可扩…

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

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

指针与函数(一)

简介 C语言中,函数的参数不仅可以是整型、实型、字符型等数据,还可以是指针类型。它的作用是将一个地址传递给函数,函数可以通过指针访问指针所指向的对象。同样,函数的返回值可以是整型、实型、字符型,也可以是指针类型。 一 .指针作函数的参数 指针作为函数的形参时,…

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事件查看器创建&#xff0c;包含Windows记录的事件列表&#xff0c;以专有的二进制XML格式保…

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

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

华为OD机试真题-狼羊过河-2024年OD统一考试(E卷)

题目描述 羊、狼、农夫都在岸边,当羊的数量小于狼的数量时,狼会攻击羊,农夫则会损失羊。农夫有一艘容量固定的船,能够承载固定数量的动物。 要求求出不损失羊情况下将全部羊和狼运到对岸需要的最小次数。只计算农夫去对岸的次数,回程时农夫不会运送羊和狼。备注:农夫在或农…

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

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

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

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

Vue 3.5 新特性解析

在Vue 3.5中&#xff0c;几个新特性值得关注&#xff0c;它们将极大简化和增强你的开发体验。让我们逐一深入了解这些特性。 1. defineProps支持解构 在Vue 3.5中&#xff0c;defineProps现在支持解构。你可以直接从defineProps中解构出需要的属性&#xff0c;而不必像以前一…

纯态和非纯态的理解(望指正)

纯态和非纯态的理解 量子状态的系统密度算子的特征量子态纯态混合态 纯态判别混合态判别 量子状态的系统 假设一个量子系统以概率 p i p_i pi​处于多个状态 ∣ ψ i ⟩ |\psi_i\rangle ∣ψi​⟩之一. 我们把 { p i , ∣ ψ ⟩ } \{p_i,|\psi\rangle\} {pi​,∣ψ⟩}称为一个…

Python中字典推导式(Dict Comprehension)是如何工作的

Python中的字典推导式&#xff08;Dict Comprehension&#xff09;是一种简洁而强大的构造字典的方式。它允许你从现有的可迭代对象&#xff08;如列表、元组、集合或另一个字典&#xff09;中快速生成新的字典&#xff0c;通过对元素应用一个表达式来设置键和值。字典推导式不…

Android Home应用程序启动流程

Android系统在启动时安装应用程序的过程&#xff0c;这些应用程序安装好之后&#xff0c;还需要有一个Home应用程序来负责把它们在桌面上展示出来&#xff0c;在Android系统中&#xff0c;这个默认的Home应用程序就是Launcher了&#xff0c;本文将详细分析Launcher应用程序的启…