这个新的 Python Web 框架改变了一切

这个新的 Python Web 框架改变了一切

Barry Lv6

纯 Python 的现代 Web 应用程序

FastHTML 是一个新的框架,用于创建纯 Python 交互式 Web 应用程序,基于 HTMX 重新流行起来的新设计理念。

简单易用

目前流行的用于制作网络应用程序的框架包括 Nextjs、Laravel 或 Django。这些框架相当复杂,需要大量的知识和时间。

FastHTML 非常简单,应该能减轻由于设计简单而带来的心理压力。

这是一个实现身份验证、CRUD 等功能的 TODO 应用程序的完整源代码。

仅使用 Python 编写

正如您所看到的,HTML 似乎是 Python 类型,这让我感到非常有趣。最近我使用类似的东西通过 Filament 构建了一个表单。这可能会引起争议,因为它不再是真正的标记,但我将在接下来的几周里尝试使用它,以查看它在更大应用程序中的表现。

但这是正确的选择吗?如果我想使用像 TailwindUI 这样的东西并复制 HTML 组件,会有办法将 HTML 插入到结构中吗?那么 JS 将如何参与其中呢?

在一个名为代码编辑器的示例中,有一种类型称为脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from fasthtml.common import *
from .toolbar import Toolbar

editor_script = Script("""
let editor;
let completionTippy;
let currentCompletion = '';

function initEditor() {
editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
editor.setOptions({
fontSize: "14px",
showPrintMargin: false,
showGutter: true,
highlightActiveLine: true,
wrap: true
});

editor.setValue("// Your code here");

window.addEventListener('resize', function() {
editor.resize();
});

document.getElementById('language').addEventListener('change', function(e) {
let mode = "ace/mode/" + e.target.value;
editor.session.setMode(mode);
});

editor.session.on('change', function(delta) {
if (delta.action === 'insert' && (delta.lines[0] === '.' || delta.lines[0] === ' ')) {
showCompletionSuggestion();
}
});

completionTippy = tippy(document.getElementById('editor'), {
content: 'Loading...',
trigger: 'manual',
placement: 'top-start',
arrow: true,
interactive: true
});

// Override the default tab behavior
editor.commands.addCommand({
name: 'insertCompletion',
bindKey: {win: 'Tab', mac: 'Tab'},
exec: function(editor) {
if (currentCompletion) {
editor.insert(currentCompletion);
currentCompletion = '';
completionTippy.hide();
} else {
editor.indent();
}
}
});
}

async function showCompletionSuggestion() {
const cursorPosition = editor.getCursorPosition();
const screenPosition = editor.renderer.textToScreenCoordinates(cursorPosition.row, cursorPosition.column);

completionTippy.setContent('Loading...');
completionTippy.setProps({
getReferenceClientRect: () => ({
width: 0,
height: 0,
top: screenPosition.pageY,
bottom: screenPosition.pageY,
left: screenPosition.pageX,
right: screenPosition.pageX,
})
});
completionTippy.show();

try {
const response = await fetch('/complete', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code: editor.getValue(),
row: cursorPosition.row,
column: cursorPosition.column
}),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
currentCompletion = data.completion;
completionTippy.setContent(`${currentCompletion} (Press Tab to insert)`);
} catch (error) {
console.error('Error:', error);
completionTippy.setContent('Error fetching completion');
currentCompletion = '';
}

setTimeout(() => {
if (currentCompletion) {
completionTippy.hide();
currentCompletion = '';
}
}, 5000);
}

document.addEventListener('DOMContentLoaded', initEditor);
""")

def CodeEditor():
return (
Div(
Toolbar(),
Div(
Div(id="editor", cls="w-full h-full"),
Script("""
me().on('contextmenu', ev => {
ev.preventDefault()
me('#context-menu').send('show', {x: ev.pageX, y: ev.pageY})
})
"""),
cls="flex-grow w-full"
),
cls="flex flex-col h-screen w-full"
),
editor_script
)

然后您可以通过返回它将脚本插入文档中。

HTMX

这正是让这个项目神奇的所在。它使得轻松创建单页应用成为可能,而无需使用像 React 这样庞大的框架。

  • 标题: 这个新的 Python Web 框架改变了一切
  • 作者: Barry
  • 创建于 : 2024-07-30 07:53:06
  • 更新于 : 2024-08-31 06:59:45
  • 链接: https://wx.role.fun/2024/07/30/e141ab032ed84f8784fd41fcf6e88d34/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
这个新的 Python Web 框架改变了一切