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/pystructui/integrations.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
integrations.py · 4.9 KB · 167 lines
1"""
2Framework integrations for PyStructUI
3"""
4
5from functools import wraps
6from typing import Dict, Any, Optional
7from .core import PresentationLayer
8from .renderers import BootstrapRenderer
9
10
11# Flask Integration
12def flask_integration(app, renderer='bootstrap'):
13 """
14 Integrate PyStructUI with Flask app
15
16 Usage:
17 from flask import Flask
18 from pystructui import flask_integration
19
20 app = Flask(__name__)
21 ui = flask_integration(app)
22
23 @app.route('/')
24 @ui.render
25 def index():
26 return {
27 'type': 'page',
28 'title': 'My Flask App',
29 'components': [
30 {'type': 'hero', 'title': 'Welcome!'},
31 {'type': 'card', 'title': 'Hello', 'body': 'From PyStructUI'}
32 ]
33 }
34 """
35 pl = PresentationLayer()
36
37 # Add default renderer
38 if renderer == 'bootstrap':
39 pl.add_renderer('bootstrap', BootstrapRenderer())
40
41 class FlaskUI:
42 def __init__(self, presentation_layer):
43 self.pl = presentation_layer
44
45 def render(self, f):
46 """Decorator to render data structures as HTML"""
47 @wraps(f)
48 def decorated_function(*args, **kwargs):
49 data = f(*args, **kwargs)
50 if isinstance(data, dict):
51 return self.pl.render(data)
52 return data
53 return decorated_function
54
55 def component(self, component_data: Dict[str, Any]):
56 """Render a single component"""
57 return self.pl.render(component_data)
58
59 return FlaskUI(pl)
60
61
62# Django Integration
63def django_integration(renderer='bootstrap'):
64 """
65 Integrate PyStructUI with Django views
66
67 Usage in views.py:
68 from django.http import HttpResponse
69 from pystructui import django_integration
70
71 ui = django_integration()
72
73 def index(request):
74 data = {
75 'type': 'page',
76 'title': 'My Django App',
77 'components': [
78 {'type': 'navbar', 'brand': 'Django + PyStructUI'},
79 {'type': 'hero', 'title': 'Welcome!'},
80 ]
81 }
82 return HttpResponse(ui.render(data))
83
84 Or as a class-based view:
85 from django.views import View
86
87 class IndexView(View):
88 def get(self, request):
89 data = {...}
90 return HttpResponse(ui.render(data))
91 """
92 pl = PresentationLayer()
93
94 if renderer == 'bootstrap':
95 pl.add_renderer('bootstrap', BootstrapRenderer())
96
97 class DjangoUI:
98 def __init__(self, presentation_layer):
99 self.pl = presentation_layer
100
101 def render(self, data: Dict[str, Any], renderer: Optional[str] = None) -> str:
102 """Render data structure to HTML"""
103 return self.pl.render(data, renderer)
104
105 def render_response(self, data: Dict[str, Any], renderer: Optional[str] = None):
106 """Render and return HttpResponse"""
107 from django.http import HttpResponse
108 html = self.render(data, renderer)
109 return HttpResponse(html)
110
111 def template_view(self, data_func):
112 """Decorator for Django class-based views"""
113 from django.http import HttpResponse
114 from django.views import View
115
116 class UIView(View):
117 def get(self, request, *args, **kwargs):
118 data = data_func(request, *args, **kwargs)
119 return HttpResponse(self.pl.render(data))
120
121 return UIView
122
123 return DjangoUI(pl)
124
125
126# FastAPI Integration
127def fastapi_integration(renderer='bootstrap'):
128 """
129 Integrate PyStructUI with FastAPI
130
131 Usage:
132 from fastapi import FastAPI
133 from fastapi.responses import HTMLResponse
134 from pystructui import fastapi_integration
135
136 app = FastAPI()
137 ui = fastapi_integration()
138
139 @app.get("/", response_class=HTMLResponse)
140 async def index():
141 data = {
142 'type': 'page',
143 'title': 'FastAPI App',
144 'components': [...]
145 }
146 return ui.render(data)
147 """
148 pl = PresentationLayer()
149
150 if renderer == 'bootstrap':
151 pl.add_renderer('bootstrap', BootstrapRenderer())
152
153 class FastAPIUI:
154 def __init__(self, presentation_layer):
155 self.pl = presentation_layer
156
157 def render(self, data: Dict[str, Any], renderer: Optional[str] = None) -> str:
158 """Render data structure to HTML"""
159 return self.pl.render(data, renderer)
160
161 def response(self, data: Dict[str, Any], renderer: Optional[str] = None):
162 """Return FastAPI HTMLResponse"""
163 from fastapi.responses import HTMLResponse
164 html = self.render(data, renderer)
165 return HTMLResponse(content=html)
166
167 return FastAPIUI(pl)