Transform data structures into HTML for any Python web framework. Works with Flask, Django, FastAPI. No templates needed - just data. Switch between Bootstrap, Tailwind, Material UI with one line.

0 downloads

PyStructUI/examples/flask_example.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/PyStructUI.git
HTTPS
https://gitstable.com/git/askrobots/PyStructUI.git
SSH
ssh://git@gitstable.com:2222/askrobots/PyStructUI.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 feat: Package restructure and PyPI release 0.1.0 616b847 1 year, 2 months ago History
flask_example.py · 4.6 KB · 171 lines
1"""
2Flask + PyStructUI Example
3Shows how to use data structures instead of HTML templates
4"""
5
6from flask import Flask, request, redirect, url_for
7from pystructui import flask_integration
8
9app = Flask(__name__)
10ui = flask_integration(app, renderer='bootstrap')
11
12# Simulate a database
13todos = []
14
15
16@app.route('/')
17@ui.render
18def index():
19 """Home page using data structures instead of HTML"""
20 return {
21 'type': 'page',
22 'title': 'Flask + PyStructUI Demo',
23 'components': [
24 {
25 'type': 'navbar',
26 'brand': 'Todo App',
27 'links': [
28 {'text': 'Home', 'url': '/'},
29 {'text': 'About', 'url': '/about'},
30 ]
31 },
32 {
33 'type': 'hero',
34 'title': 'Welcome to PyStructUI',
35 'subtitle': 'Build UIs with data structures, not HTML',
36 'variant': 'primary'
37 },
38 {
39 'type': 'card',
40 'title': '📝 Todo List',
41 'body': {
42 'type': 'raw',
43 'content': _render_todo_list()
44 },
45 'footer': {
46 'type': 'button',
47 'text': 'Add Todo',
48 'variant': 'success',
49 'onclick': 'location.href="/add"'
50 }
51 }
52 ]
53 }
54
55
56def _render_todo_list():
57 """Render todo list as HTML"""
58 if not todos:
59 return '<p>No todos yet. Add one!</p>'
60
61 items = ''.join([
62 f'<li class="list-group-item d-flex justify-content-between">'
63 f'{todo["text"]}'
64 f'<a href="/delete/{i}" class="btn btn-sm btn-danger">Delete</a>'
65 f'</li>'
66 for i, todo in enumerate(todos)
67 ])
68
69 return f'<ul class="list-group">{items}</ul>'
70
71
72@app.route('/add')
73@ui.render
74def add_todo_form():
75 """Add todo form - pure data structure"""
76 return {
77 'type': 'page',
78 'title': 'Add Todo',
79 'components': [
80 {
81 'type': 'navbar',
82 'brand': 'Todo App',
83 },
84 {
85 'type': 'card',
86 'title': 'Add New Todo',
87 'body': {
88 'type': 'form',
89 'method': 'POST',
90 'action': '/add',
91 'fields': [
92 {
93 'type': 'text',
94 'name': 'todo_text',
95 'label': 'Todo Text',
96 'placeholder': 'Enter your todo...',
97 'required': True
98 }
99 ]
100 }
101 }
102 ]
103 }
104
105
106@app.route('/add', methods=['POST'])
107def add_todo_post():
108 """Handle form submission"""
109 todo_text = request.form.get('todo_text')
110 if todo_text:
111 todos.append({'text': todo_text})
112 return redirect(url_for('index'))
113
114
115@app.route('/delete/<int:index>')
116def delete_todo(index):
117 """Delete a todo"""
118 if 0 <= index < len(todos):
119 todos.pop(index)
120 return redirect(url_for('index'))
121
122
123@app.route('/about')
124@ui.render
125def about():
126 """About page demonstrating different components"""
127 return {
128 'type': 'page',
129 'title': 'About PyStructUI',
130 'components': [
131 {
132 'type': 'navbar',
133 'brand': 'Todo App',
134 'links': [
135 {'text': 'Home', 'url': '/'},
136 {'text': 'About', 'url': '/about'},
137 ]
138 },
139 {
140 'type': 'grid',
141 'columns': 3,
142 'items': [
143 {
144 'type': 'card',
145 'title': '🚀 Fast',
146 'body': 'No template compilation, just data transformation'
147 },
148 {
149 'type': 'card',
150 'title': '🎨 Flexible',
151 'body': 'Switch UI frameworks without changing code'
152 },
153 {
154 'type': 'card',
155 'title': '🔧 Simple',
156 'body': 'Define UI as data structures, not HTML'
157 }
158 ]
159 },
160 {
161 'type': 'alert',
162 'message': 'This entire app has ZERO HTML templates!',
163 'variant': 'success',
164 'dismissible': True
165 }
166 ]
167 }
168
169
170if __name__ == '__main__':
171 app.run(debug=True, port=5000)