tindo:
A micro-framework based on wsgiref
pip install tindoget decorator: for HTTP GET Methodpost decorator: for HTTP POST Method- route decorator: for HTTP GET and POST methods
- Route class: wrap the decorated functions, which contains the
match(self, url)methods to determinate whether the url match the route nor not. - Request class: wrap the wsgi's
environdictionary into more useful apis, includingmethods,cookies,pathandinputsetc. - Response class: a high-level of wsgi's
statusandresponse_headers. - view decorator: a specific html template to render
- Tindo class: main class of web framework. To approach the multi-thread environment, use threading module'
s
Localclass.
The example folder contains the minimum demo using tindo.
- example
- static
- style.css
- templates
- index.html
- name.html
- register.html
- comment.html
- register.html
- registered.html
- session.html
- app.py
- urls.py
This module defines the main routes rules of web application,
just like Django.
@view('index.html')
@route('/')
def index():
return dict()Using get decorator to decorate a function. Using view
decorator to decide which template to be rendered.
If no variables in the template, just return a dict.
@view('register.html')
@route('/register', methods=['GET', 'POST'])
def register():
i = ctx.request.input(firstname=None, lastname=None)
firstname = i.get('firstname')
lastname = i.get('lastname')
if firstname is None and lastname is None:
return dict(register=True)
else:
return dict(firstname=firstname, lastname=lastname)The thread-safe ctx variable includes current request, which
contains posted values in wsgi.input dictionary.
@view('name.html')
@route('/user/<username>')
def user(name):
return dict(name=name)
@view('comment.html')
@route('/user/<name>/<group>')
def comment(name, group):
return dict(name=name, group=group)Referring flask route, url can contain a query variable. If
get decorator with <variable>, the route will convert the
url's last component as parameter and invoke the function.
Session is used to keep identification of individual client.
This article
points out how session works. In tindo, the application contain a global
dictionary that keeps every session-id. Each session is a dictionary instance in memory.
@view('session.html')
@route('/session')
def session():
sess = ctx.response.session
return dict(name=sess.name)This module is main part of web application. Just import tindo and
make Tindo instance and run it. But it needs to import url module.
import os
from tindo import Tindo
import urls
app = Tindo(os.path.dirname(os.path.abspath(__file__)))
app.add_module(urls)
if __name__ == '__main__':
app.run()- Export it to Python 3.x
- Add session feature
- Refactor
- update
routedecorator and deprecategetandpostdecorators. 17. Sep. 2017 - re-organize
tindo.pywsgi protocol using__call__. 18. Sep. 2017 - add
localmodule which behave likethreading.local. 19. Sep. 2017 - add
manage.pyscript to controltestandapp, refactor the uglyreload(sys)statement, and use absolute import features. 20. Sep. 2017 - add Tindo's
__init__.pyto make lessimportstatement.