v2 (#186)
This commit is contained in:
@@ -4,7 +4,7 @@ app = Microdot()
|
||||
|
||||
|
||||
@app.get('/')
|
||||
def index(req):
|
||||
async def index(req):
|
||||
return {'hello': 'world'}
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from microdot_asgi import Microdot
|
||||
from microdot.asgi import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.get('/')
|
||||
async def index(req):
|
||||
return {'hello': 'world'}
|
||||
|
||||
|
||||
app.run()
|
||||
@@ -4,5 +4,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get('/')
|
||||
def index():
|
||||
async def index():
|
||||
return {'hello': 'world'}
|
||||
|
||||
@@ -4,5 +4,5 @@ app = Quart(__name__)
|
||||
|
||||
|
||||
@app.get('/')
|
||||
def index():
|
||||
async def index():
|
||||
return {'hello': 'world'}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from microdot_wsgi import Microdot
|
||||
from microdot.wsgi import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
9
examples/benchmark/requirements.in
Normal file
9
examples/benchmark/requirements.in
Normal file
@@ -0,0 +1,9 @@
|
||||
pip-tools
|
||||
flask
|
||||
quart
|
||||
fastapi
|
||||
gunicorn
|
||||
uvicorn
|
||||
requests
|
||||
psutil
|
||||
humanize
|
||||
@@ -1,33 +1,115 @@
|
||||
aiofiles==0.8.0
|
||||
anyio==3.6.1
|
||||
blinker==1.5
|
||||
certifi==2023.7.22
|
||||
charset-normalizer==2.1.0
|
||||
click==8.1.3
|
||||
fastapi==0.79.0
|
||||
Flask==2.3.2
|
||||
gunicorn==20.1.0
|
||||
h11==0.13.0
|
||||
#
|
||||
# This file is autogenerated by pip-compile with Python 3.12
|
||||
# by the following command:
|
||||
#
|
||||
# pip-compile requirements.in
|
||||
#
|
||||
aiofiles==23.2.1
|
||||
# via quart
|
||||
annotated-types==0.6.0
|
||||
# via pydantic
|
||||
anyio==3.7.1
|
||||
# via
|
||||
# fastapi
|
||||
# starlette
|
||||
blinker==1.7.0
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
build==1.0.3
|
||||
# via pip-tools
|
||||
certifi==2023.11.17
|
||||
# via requests
|
||||
charset-normalizer==3.3.2
|
||||
# via requests
|
||||
click==8.1.7
|
||||
# via
|
||||
# flask
|
||||
# pip-tools
|
||||
# quart
|
||||
# uvicorn
|
||||
fastapi==0.104.1
|
||||
# via -r requirements.in
|
||||
flask==3.0.0
|
||||
# via
|
||||
# -r requirements.in
|
||||
# quart
|
||||
gunicorn==21.2.0
|
||||
# via -r requirements.in
|
||||
h11==0.14.0
|
||||
# via
|
||||
# hypercorn
|
||||
# uvicorn
|
||||
# wsproto
|
||||
h2==4.1.0
|
||||
# via hypercorn
|
||||
hpack==4.0.0
|
||||
humanize==4.3.0
|
||||
hypercorn==0.13.2
|
||||
# via h2
|
||||
humanize==4.9.0
|
||||
# via -r requirements.in
|
||||
hypercorn==0.15.0
|
||||
# via quart
|
||||
hyperframe==6.0.1
|
||||
idna==3.3
|
||||
# via h2
|
||||
idna==3.6
|
||||
# via
|
||||
# anyio
|
||||
# requests
|
||||
itsdangerous==2.1.2
|
||||
Jinja2==3.1.2
|
||||
MarkupSafe==2.1.1
|
||||
microdot
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
jinja2==3.1.2
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
markupsafe==2.1.3
|
||||
# via
|
||||
# jinja2
|
||||
# quart
|
||||
# werkzeug
|
||||
packaging==23.2
|
||||
# via
|
||||
# build
|
||||
# gunicorn
|
||||
pip-tools==7.3.0
|
||||
# via -r requirements.in
|
||||
priority==2.0.0
|
||||
psutil==5.9.1
|
||||
pydantic==1.9.1
|
||||
quart==0.18.0
|
||||
# via hypercorn
|
||||
psutil==5.9.6
|
||||
# via -r requirements.in
|
||||
pydantic==2.5.2
|
||||
# via fastapi
|
||||
pydantic-core==2.14.5
|
||||
# via pydantic
|
||||
pyproject-hooks==1.0.0
|
||||
# via build
|
||||
quart==0.19.4
|
||||
# via -r requirements.in
|
||||
requests==2.31.0
|
||||
sniffio==1.2.0
|
||||
# via -r requirements.in
|
||||
sniffio==1.3.0
|
||||
# via anyio
|
||||
starlette==0.27.0
|
||||
toml==0.10.2
|
||||
typing_extensions==4.3.0
|
||||
urllib3==1.26.18
|
||||
uvicorn==0.18.2
|
||||
Werkzeug==2.2.3
|
||||
wsproto==1.1.0
|
||||
# via fastapi
|
||||
typing-extensions==4.9.0
|
||||
# via
|
||||
# fastapi
|
||||
# pydantic
|
||||
# pydantic-core
|
||||
urllib3==2.1.0
|
||||
# via requests
|
||||
uvicorn==0.24.0.post1
|
||||
# via -r requirements.in
|
||||
werkzeug==3.0.1
|
||||
# via
|
||||
# flask
|
||||
# quart
|
||||
wheel==0.42.0
|
||||
# via pip-tools
|
||||
wsproto==1.2.0
|
||||
# via hypercorn
|
||||
|
||||
# The following packages are considered to be unsafe in a requirements file:
|
||||
# pip
|
||||
# setuptools
|
||||
|
||||
@@ -14,13 +14,8 @@ apps = [
|
||||
),
|
||||
(
|
||||
'micropython mem.py',
|
||||
{'MICROPYPATH': '../../src'},
|
||||
'microdot-micropython-sync'
|
||||
),
|
||||
(
|
||||
'micropython mem_async.py',
|
||||
{'MICROPYPATH': '../../src:../../libs/micropython'},
|
||||
'microdot-micropython-async'
|
||||
'microdot-micropython'
|
||||
),
|
||||
(
|
||||
['python', '-c', 'import time; time.sleep(10)'],
|
||||
@@ -30,47 +25,42 @@ apps = [
|
||||
(
|
||||
'python mem.py',
|
||||
{'PYTHONPATH': '../../src'},
|
||||
'microdot-cpython-sync'
|
||||
),
|
||||
(
|
||||
'python mem_async.py',
|
||||
{'PYTHONPATH': '../../src'},
|
||||
'microdot-cpython-async'
|
||||
),
|
||||
(
|
||||
'gunicorn --workers 1 --bind :5000 mem_wsgi:app',
|
||||
{'PYTHONPATH': '../../src'},
|
||||
'microdot-gunicorn-sync'
|
||||
'microdot-cpython'
|
||||
),
|
||||
(
|
||||
'uvicorn --workers 1 --port 5000 mem_asgi:app',
|
||||
{'PYTHONPATH': '../../src'},
|
||||
'microdot-uvicorn-async'
|
||||
'microdot-uvicorn'
|
||||
),
|
||||
(
|
||||
'gunicorn --workers 1 --bind :5000 mem_wsgi:app',
|
||||
{'PYTHONPATH': '../../src'},
|
||||
'microdot-gunicorn'
|
||||
),
|
||||
(
|
||||
'flask run',
|
||||
{'FLASK_APP': 'mem_flask.py'},
|
||||
'flask-run-sync'
|
||||
'flask-run'
|
||||
),
|
||||
(
|
||||
'quart run',
|
||||
{'QUART_APP': 'mem_quart.py'},
|
||||
'quart-run-async'
|
||||
'quart-run'
|
||||
),
|
||||
(
|
||||
'gunicorn --workers 1 --bind :5000 mem_flask:app',
|
||||
{},
|
||||
'flask-gunicorn-sync'
|
||||
'flask-gunicorn'
|
||||
),
|
||||
(
|
||||
'uvicorn --workers 1 --port 5000 mem_quart:app',
|
||||
{},
|
||||
'quart-uvicorn-async'
|
||||
'quart-uvicorn'
|
||||
),
|
||||
(
|
||||
'uvicorn --workers 1 --port 5000 mem_fastapi:app',
|
||||
{},
|
||||
'fastapi-uvicorn-async'
|
||||
'fastapi-uvicorn'
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from microdot import Microdot
|
||||
from microdot_cors import CORS
|
||||
from microdot.cors import CORS
|
||||
|
||||
app = Microdot()
|
||||
CORS(app, allowed_origins=['https://example.org'], allow_credentials=True)
|
||||
|
||||
@@ -2,7 +2,7 @@ from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
html = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
@@ -20,12 +20,12 @@ htmldoc = '''<!DOCTYPE html>
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
async def hello(request):
|
||||
return html, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
def shutdown(request):
|
||||
async def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from microdot_asgi import Microdot
|
||||
from microdot.asgi import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
html = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
@@ -21,7 +21,7 @@ htmldoc = '''<!DOCTYPE html>
|
||||
|
||||
@app.route('/')
|
||||
async def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
return html, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h1>Microdot Example Page</h1>
|
||||
<p>Hello from Microdot!</p>
|
||||
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
@app.route('/')
|
||||
async def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
async def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
|
||||
|
||||
app.run(debug=True)
|
||||
@@ -1,8 +1,8 @@
|
||||
from microdot_wsgi import Microdot
|
||||
from microdot.wsgi import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
html = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
@@ -21,7 +21,7 @@ htmldoc = '''<!DOCTYPE html>
|
||||
|
||||
@app.route('/')
|
||||
def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
return html, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from microdot import Microdot, Response, redirect
|
||||
from microdot_session import set_session_secret_key, with_session, \
|
||||
update_session, delete_session
|
||||
from microdot.session import Session, with_session
|
||||
|
||||
BASE_TEMPLATE = '''<!doctype html>
|
||||
<html>
|
||||
@@ -29,18 +28,19 @@ LOGGED_IN = '''<p>Hello <b>{username}</b>!</p>
|
||||
</form>'''
|
||||
|
||||
app = Microdot()
|
||||
set_session_secret_key('top-secret')
|
||||
Session(app, secret_key='top-secret')
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.get('/')
|
||||
@app.post('/')
|
||||
@with_session
|
||||
def index(req, session):
|
||||
async def index(req, session):
|
||||
username = session.get('username')
|
||||
if req.method == 'POST':
|
||||
username = req.form.get('username')
|
||||
update_session(req, {'username': username})
|
||||
session['username'] = username
|
||||
session.save()
|
||||
return redirect('/')
|
||||
if username is None:
|
||||
return BASE_TEMPLATE.format(content=LOGGED_OUT)
|
||||
@@ -50,8 +50,9 @@ def index(req, session):
|
||||
|
||||
|
||||
@app.post('/logout')
|
||||
def logout(req):
|
||||
delete_session(req)
|
||||
@with_session
|
||||
async def logout(req, session):
|
||||
session.delete()
|
||||
return redirect('/')
|
||||
|
||||
|
||||
|
||||
16
examples/sse/counter.py
Normal file
16
examples/sse/counter.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import asyncio
|
||||
from microdot import Microdot
|
||||
from microdot.sse import with_sse
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/events')
|
||||
@with_sse
|
||||
async def events(request, sse):
|
||||
for i in range(10):
|
||||
await asyncio.sleep(1)
|
||||
await sse.send({'counter': i})
|
||||
|
||||
|
||||
app.run(debug=True)
|
||||
@@ -1,15 +1,14 @@
|
||||
from microdot import Microdot, send_file
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
async def index(request):
|
||||
return send_file('static/index.html')
|
||||
|
||||
|
||||
@app.route('/static/<path:path>')
|
||||
def static(request, path):
|
||||
async def static(request, path):
|
||||
if '..' in path:
|
||||
# directory traversal is not allowed
|
||||
return 'Not found', 404
|
||||
|
||||
10
examples/static/static/index.css
Normal file
10
examples/static/static/index.css
Normal file
@@ -0,0 +1,10 @@
|
||||
p {
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
color: #3070b3;
|
||||
text-align: center;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
from microdot_asyncio import Microdot, send_file
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
async def index(request):
|
||||
return send_file('static/index.html')
|
||||
|
||||
|
||||
@app.route('/static/<path:path>')
|
||||
async def static(request, path):
|
||||
if '..' in path:
|
||||
# directory traversal is not allowed
|
||||
return 'Not found', 404
|
||||
return send_file('static/' + path)
|
||||
|
||||
|
||||
app.run(debug=True)
|
||||
@@ -1,8 +1,5 @@
|
||||
try:
|
||||
import utime as time
|
||||
except ImportError:
|
||||
import time
|
||||
|
||||
import sys
|
||||
import asyncio
|
||||
from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
@@ -14,7 +11,7 @@ for file in ['1.jpg', '2.jpg', '3.jpg']:
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
async def index(request):
|
||||
return '''<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
@@ -29,14 +26,38 @@ def index(request):
|
||||
|
||||
|
||||
@app.route('/video_feed')
|
||||
def video_feed(request):
|
||||
def stream():
|
||||
yield b'--frame\r\n'
|
||||
while True:
|
||||
for frame in frames:
|
||||
yield b'Content-Type: image/jpeg\r\n\r\n' + frame + \
|
||||
b'\r\n--frame\r\n'
|
||||
time.sleep(1)
|
||||
async def video_feed(request):
|
||||
print('Starting video stream.')
|
||||
|
||||
if sys.implementation.name != 'micropython':
|
||||
# CPython supports async generator function
|
||||
async def stream():
|
||||
try:
|
||||
yield b'--frame\r\n'
|
||||
while True:
|
||||
for frame in frames:
|
||||
yield b'Content-Type: image/jpeg\r\n\r\n' + frame + \
|
||||
b'\r\n--frame\r\n'
|
||||
await asyncio.sleep(1)
|
||||
except GeneratorExit:
|
||||
print('Stopping video stream.')
|
||||
else:
|
||||
# MicroPython can only use class-based async generators
|
||||
class stream():
|
||||
def __init__(self):
|
||||
self.i = 0
|
||||
|
||||
def __aiter__(self):
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
await asyncio.sleep(1)
|
||||
self.i = (self.i + 1) % len(frames)
|
||||
return b'Content-Type: image/jpeg\r\n\r\n' + \
|
||||
frames[self.i] + b'\r\n--frame\r\n'
|
||||
|
||||
async def aclose(self):
|
||||
print('Stopping video stream.')
|
||||
|
||||
return stream(), 200, {'Content-Type':
|
||||
'multipart/x-mixed-replace; boundary=frame'}
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
import sys
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
import asyncio
|
||||
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
frames = []
|
||||
for file in ['1.jpg', '2.jpg', '3.jpg']:
|
||||
with open(file, 'rb') as f:
|
||||
frames.append(f.read())
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
return '''<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Video Streaming</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Microdot Video Streaming</h1>
|
||||
<img src="/video_feed">
|
||||
</body>
|
||||
</html>''', 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/video_feed')
|
||||
async def video_feed(request):
|
||||
if sys.implementation.name != 'micropython':
|
||||
# CPython supports yielding async generators
|
||||
async def stream():
|
||||
yield b'--frame\r\n'
|
||||
while True:
|
||||
for frame in frames:
|
||||
yield b'Content-Type: image/jpeg\r\n\r\n' + frame + \
|
||||
b'\r\n--frame\r\n'
|
||||
await asyncio.sleep(1)
|
||||
|
||||
else:
|
||||
# MicroPython can only use class-based async generators
|
||||
class stream():
|
||||
def __init__(self):
|
||||
self.i = 0
|
||||
|
||||
def __aiter__(self):
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
await asyncio.sleep(1)
|
||||
self.i = (self.i + 1) % len(frames)
|
||||
return b'Content-Type: image/jpeg\r\n\r\n' + \
|
||||
frames[self.i] + b'\r\n--frame\r\n'
|
||||
|
||||
return stream(), 200, {'Content-Type':
|
||||
'multipart/x-mixed-replace; boundary=frame'}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
18
examples/templates/jinja/async_template.py
Normal file
18
examples/templates/jinja/async_template.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot.jinja import template, init_templates
|
||||
|
||||
init_templates('templates', enable_async=True)
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return await template('index.html').render_async(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
@@ -1,18 +1,18 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot_jinja import render_template
|
||||
from microdot.jinja import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return render_template('page1.html', page='Page 1')
|
||||
async def index(req):
|
||||
return template('page1.html').render(page='Page 1')
|
||||
|
||||
|
||||
@app.route('/page2')
|
||||
def page2(req):
|
||||
return render_template('page2.html', page='Page 2')
|
||||
async def page2(req):
|
||||
return template('page2.html').render(page='Page 2')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot_jinja import render_template
|
||||
from microdot.jinja import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index(req):
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return render_template('index.html', name=name)
|
||||
return template('index.html').render(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from microdot_asyncio import Microdot, Response
|
||||
from microdot_utemplate import render_template
|
||||
from microdot.asgi import Microdot, Response
|
||||
from microdot.jinja import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
@@ -10,7 +10,7 @@ async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return render_template('index.html', name=name)
|
||||
return template('index.html').render(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
17
examples/templates/jinja/hello_wsgi.py
Normal file
17
examples/templates/jinja/hello_wsgi.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from microdot.wsgi import Microdot, Response
|
||||
from microdot.jinja import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return template('index.html').render(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
17
examples/templates/jinja/streaming.py
Normal file
17
examples/templates/jinja/streaming.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot.jinja import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return template('index.html').generate(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
17
examples/templates/utemplate/async_template.py
Normal file
17
examples/templates/utemplate/async_template.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot.utemplate import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return await template('index.html').render_async(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
@@ -1,18 +1,18 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot_utemplate import render_template
|
||||
from microdot.utemplate import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(req):
|
||||
return render_template('page1.html', page='Page 1')
|
||||
async def index(req):
|
||||
return template('page1.html').render(page='Page 1')
|
||||
|
||||
|
||||
@app.route('/page2')
|
||||
def page2(req):
|
||||
return render_template('page2.html', page='Page 2')
|
||||
async def page2(req):
|
||||
return template('page2.html').render(page='Page 2')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot_utemplate import render_template
|
||||
from microdot.utemplate import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index(req):
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return render_template('index.html', name=name)
|
||||
return template('index.html').render(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
17
examples/templates/utemplate/hello_asgi.py
Normal file
17
examples/templates/utemplate/hello_asgi.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from microdot.asgi import Microdot, Response
|
||||
from microdot.utemplate import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return template('index.html').render(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
17
examples/templates/utemplate/hello_wsgi.py
Normal file
17
examples/templates/utemplate/hello_wsgi.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from microdot.wsgi import Microdot, Response
|
||||
from microdot.utemplate import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return template('index.html').render(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
17
examples/templates/utemplate/streaming.py
Normal file
17
examples/templates/utemplate/streaming.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from microdot import Microdot, Response
|
||||
from microdot.utemplate import template
|
||||
|
||||
app = Microdot()
|
||||
Response.default_content_type = 'text/html'
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
async def index(req):
|
||||
name = None
|
||||
if req.method == 'POST':
|
||||
name = req.form.get('name')
|
||||
return template('index.html').generate(name=name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
@@ -1,23 +0,0 @@
|
||||
import ssl
|
||||
from microdot_asyncio import Microdot, send_file
|
||||
from microdot_asyncio_websocket import with_websocket
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@app.route('/echo')
|
||||
@with_websocket
|
||||
async def echo(request, ws):
|
||||
while True:
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
|
||||
|
||||
sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
sslctx.load_cert_chain('cert.pem', 'key.pem')
|
||||
app.run(port=4443, debug=True, ssl=sslctx)
|
||||
@@ -1,24 +0,0 @@
|
||||
import sys
|
||||
from microdot import Microdot, send_file
|
||||
from microdot_websocket import with_websocket
|
||||
from microdot_ssl import create_ssl_context
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@app.route('/echo')
|
||||
@with_websocket
|
||||
def echo(request, ws):
|
||||
while True:
|
||||
data = ws.receive()
|
||||
ws.send(data)
|
||||
|
||||
|
||||
ext = 'der' if sys.implementation.name == 'micropython' else 'pem'
|
||||
sslctx = create_ssl_context('cert.' + ext, 'key.' + ext)
|
||||
app.run(port=4443, debug=True, ssl=sslctx)
|
||||
@@ -1,9 +1,9 @@
|
||||
import ssl
|
||||
from microdot_asyncio import Microdot
|
||||
from microdot import Microdot
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
html = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
@@ -22,7 +22,7 @@ htmldoc = '''<!DOCTYPE html>
|
||||
|
||||
@app.route('/')
|
||||
async def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
return html, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
@@ -1,37 +0,0 @@
|
||||
import sys
|
||||
from microdot import Microdot
|
||||
from microdot_ssl import create_ssl_context
|
||||
|
||||
app = Microdot()
|
||||
|
||||
htmldoc = '''<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Example Page</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h1>Microdot Example Page</h1>
|
||||
<p>Hello from Microdot!</p>
|
||||
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
|
||||
|
||||
ext = 'der' if sys.implementation.name == 'micropython' else 'pem'
|
||||
sslctx = create_ssl_context('cert.' + ext, 'key.' + ext)
|
||||
app.run(port=4443, debug=True, ssl=sslctx)
|
||||
@@ -1,36 +0,0 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot TLS WebSocket Demo</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Microdot TLS WebSocket Demo</h1>
|
||||
<div id="log"></div>
|
||||
<br>
|
||||
<form id="form">
|
||||
<label for="text">Input: </label>
|
||||
<input type="text" id="text" autofocus>
|
||||
</form>
|
||||
<script>
|
||||
const log = (text, color) => {
|
||||
document.getElementById('log').innerHTML += `<span style="color: ${color}">${text}</span><br>`;
|
||||
};
|
||||
|
||||
const socket = new WebSocket('wss://' + location.host + '/echo');
|
||||
socket.addEventListener('message', ev => {
|
||||
log('<<< ' + ev.data, 'blue');
|
||||
});
|
||||
socket.addEventListener('close', ev => {
|
||||
log('<<< closed');
|
||||
});
|
||||
document.getElementById('form').onsubmit = ev => {
|
||||
ev.preventDefault();
|
||||
const textField = document.getElementById('text');
|
||||
log('>>> ' + textField.value, 'red');
|
||||
socket.send(textField.value);
|
||||
textField.value = '';
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -5,12 +5,12 @@ Request.max_content_length = 1024 * 1024 # 1MB (change as needed)
|
||||
|
||||
|
||||
@app.get('/')
|
||||
def index(request):
|
||||
async def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@app.post('/upload')
|
||||
def upload(request):
|
||||
async def upload(request):
|
||||
# obtain the filename and size from request headers
|
||||
filename = request.headers['Content-Disposition'].split(
|
||||
'filename=')[1].strip('"')
|
||||
@@ -22,7 +22,7 @@ def upload(request):
|
||||
# write the file to the files directory in 1K chunks
|
||||
with open('files/' + filename, 'wb') as f:
|
||||
while size > 0:
|
||||
chunk = request.stream.read(min(size, 1024))
|
||||
chunk = await request.stream.read(min(size, 1024))
|
||||
f.write(chunk)
|
||||
size -= len(chunk)
|
||||
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
from microdot_asyncio import Microdot, send_file, Request
|
||||
|
||||
app = Microdot()
|
||||
Request.max_content_length = 1024 * 1024 # 1MB (change as needed)
|
||||
|
||||
|
||||
@app.get('/')
|
||||
async def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@app.post('/upload')
|
||||
async def upload(request):
|
||||
# obtain the filename and size from request headers
|
||||
filename = request.headers['Content-Disposition'].split(
|
||||
'filename=')[1].strip('"')
|
||||
size = int(request.headers['Content-Length'])
|
||||
|
||||
# sanitize the filename
|
||||
filename = filename.replace('/', '_')
|
||||
|
||||
# write the file to the files directory in 1K chunks
|
||||
with open('files/' + filename, 'wb') as f:
|
||||
while size > 0:
|
||||
chunk = await request.stream.read(min(size, 1024))
|
||||
f.write(chunk)
|
||||
size -= len(chunk)
|
||||
|
||||
print('Successfully saved file: ' + filename)
|
||||
return ''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
@@ -1,20 +1,20 @@
|
||||
from microdot import Microdot, send_file
|
||||
from microdot_websocket import with_websocket
|
||||
from microdot.websocket import with_websocket
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
async def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@app.route('/echo')
|
||||
@with_websocket
|
||||
def echo(request, ws):
|
||||
async def echo(request, ws):
|
||||
while True:
|
||||
data = ws.receive()
|
||||
ws.send(data)
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
|
||||
|
||||
app.run()
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
from microdot_asgi import Microdot, send_file
|
||||
from microdot_asgi_websocket import with_websocket
|
||||
from microdot.asgi import Microdot, send_file, with_websocket
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
async def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@@ -15,3 +14,7 @@ async def echo(request, ws):
|
||||
while True:
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
from microdot_asyncio import Microdot, send_file
|
||||
from microdot_asyncio_websocket import with_websocket
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@app.route('/echo')
|
||||
@with_websocket
|
||||
async def echo(request, ws):
|
||||
while True:
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
|
||||
|
||||
app.run()
|
||||
@@ -1,17 +1,20 @@
|
||||
from microdot_wsgi import Microdot, send_file
|
||||
from microdot_websocket import with_websocket
|
||||
from microdot.wsgi import Microdot, send_file, with_websocket
|
||||
|
||||
app = Microdot()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index(request):
|
||||
async def index(request):
|
||||
return send_file('index.html')
|
||||
|
||||
|
||||
@app.route('/echo')
|
||||
@with_websocket
|
||||
def echo(request, ws):
|
||||
async def echo(request, ws):
|
||||
while True:
|
||||
data = ws.receive()
|
||||
ws.send(data)
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
Reference in New Issue
Block a user