我有 Flet 文档提供的 to_Do_app 模型,我正在尝试学习如何将其上传到 azure 服务器,以了解 Azure 的工作原理。但调查中并没有明确的信息。如果有人教我是否可能以及如何实现,我将不胜感激。
我唯一发现的是这个文本但我仍然不确定该框架目前是否与Azure不兼容。
这是我要上传的代码。
https://github.com/flet-dev/examples/tree/5c16ccc21c41c3acf9221dec8bac5127a3871b4f/python/apps/todo
我尝试了示例代码并成功将其部署到Azure应用程序服务。
我在虚拟环境中运行了以下命令:
python -m venv venv
.\venv\Scripts\activate
.
需求.txt:
flet == 0.19.0
我将此
flet.app(target=main, view=flet.AppView.WEB_BROWSER)
添加到 main.py 的末尾以作为 Web 应用程序部署。
主要。 py:
import flet
from flet import (
Checkbox,
Column,
FloatingActionButton,
IconButton,
Page,
Row,
Tab,
Tabs,
TextField,
UserControl,
colors,
icons,
)
class Task(UserControl):
def __init__(self, task_name, task_status_change, task_delete):
super().__init__()
self.completed = False
self.task_name = task_name
self.task_status_change = task_status_change
self.task_delete = task_delete
def build(self):
self.display_task = Checkbox(
value=False, label=self.task_name, on_change=self.status_changed
)
self.edit_name = TextField(expand=1)
self.display_view = Row(
alignment="spaceBetween",
vertical_alignment="center",
controls=[
self.display_task,
Row(
spacing=0,
controls=[
IconButton(
icon=icons.CREATE_OUTLINED,
tooltip="Edit To-Do",
on_click=self.edit_clicked,
),
IconButton(
icons.DELETE_OUTLINE,
tooltip="Delete To-Do",
on_click=self.delete_clicked,
),
],
),
],
)
self.edit_view = Row(
visible=False,
alignment="spaceBetween",
vertical_alignment="center",
controls=[
self.edit_name,
IconButton(
icon=icons.DONE_OUTLINE_OUTLINED,
icon_color=colors.GREEN,
tooltip="Update To-Do",
on_click=self.save_clicked,
),
],
)
return Column(controls=[self.display_view, self.edit_view])
def edit_clicked(self, e):
self.edit_name.value = self.display_task.label
self.display_view.visible = False
self.edit_view.visible = True
self.update()
def save_clicked(self, e):
self.display_task.label = self.edit_name.value
self.display_view.visible = True
self.edit_view.visible = False
self.update()
def status_changed(self, e):
self.completed = self.display_task.value
self.task_status_change(self)
def delete_clicked(self, e):
self.task_delete(self)
class TodoApp(UserControl):
def build(self):
self.new_task = TextField(hint_text="Whats needs to be done?", expand=True)
self.tasks = Column()
self.filter = Tabs(
selected_index=0,
on_change=self.tabs_changed,
tabs=[Tab(text="all"), Tab(text="active"), Tab(text="completed")],
)
# application's root control (i.e. "view") containing all other controls
return Column(
width=600,
controls=[
Row(
controls=[
self.new_task,
FloatingActionButton(icon=icons.ADD, on_click=self.add_clicked),
],
),
Column(
spacing=25,
controls=[
self.filter,
self.tasks,
],
),
],
)
def add_clicked(self, e):
task = Task(self.new_task.value, self.task_status_change, self.task_delete)
self.tasks.controls.append(task)
self.new_task.value = ""
self.update()
def task_status_change(self, task):
self.update()
def task_delete(self, task):
self.tasks.controls.remove(task)
self.update()
def update(self):
status = self.filter.tabs[self.filter.selected_index].text
for task in self.tasks.controls:
task.visible = (
status == "all"
or (status == "active" and task.completed == False)
or (status == "completed" and task.completed)
)
super().update()
def tabs_changed(self, e):
self.update()
def main(page: Page):
page.title = "ToDo App"
page.horizontal_alignment = "center"
page.update()
app = TodoApp()
page.add(app)
flet.app(target=main, view=flet.AppView.WEB_BROWSER)
我运行了以下命令:
python main.py
本地输出:
我运行下面的命令来生成一个“dist”文件夹以用于部署目的。
flet publish main.py
我在Azure门户中创建了一个Azure Web应用程序,选择运行时堆栈为Node 18,并选择操作系统为Windows,如下所示。
我通过VS Code将Web应用程序部署到Azure App Service,如下所示。
Azure 应用服务输出: