目前处于开发阶段,更新过快,文档与实际框架内容有出入,暂勿使用
A tiny and easy python web framework just for your fun! :)
Hotpot is designed almost just for JSON RESTful, but for sure, you can render template or transform data with xml as you want.
By the way, if you never know the hotpot, you may miss one of the most delicious,fragrant and tasty food in the world ~ _~
Hopefully, Hotpot will be designed as a fast RESTful web framework.
Hotpot is still in the development stage.
中文简体: 还没开始写。。欢迎来帮忙!
install and update hotpot using pip:
pip install hotpot
from hotpot.app import Hotpot
from hotpot.wrappers import Request
app = Hotpot()
@app.route("/")
def index(_app: Hotpot, request: Request):
return {"index": True}
if __name__ == "__main__":
# app will automatically run on localhost:8080
app.run()
For more examples, please read the Examples.
In this tutorial, we will build a simple poll web.
Note: Only list a few useful and important API which might be enough to build a simple app.
Parameters:
- main_app: describe whether app is the main app. If True, the app is main app, else, the app is not main app which will be combined to the main app, in order to make a whole app.
- name: the name of the app, which is used for namespace of endpoints or view functions.
- base_rule: default is "/", app's route rules will be built on base_rule.
Functions:
-
combine_app(self, other_app: 'Hotpot'):
- Description: combine main app with other app to make a whole app.
- Parameters:
- other_app: other app which should not be main app.
- Return: None
-
run(self)
- Description: simple WSGI Server for development other than production.
- Return: None
-
add_config(self, config: Union[dict, str])
- Description: add a new config or cover the previous config.
- Parameters:
- config: can be a dict or the path of a config file which is formatted as ini file (e.g. config.ini).
- Return: None
Decorators:
-
route(self, rule, endpoint: str = None, **options):
- Description: map rule to endpoint (or say, view function).
- Parameters:
- rule: rule of route, e.g. "/"
- endpoint: rule will be mapped to the endpoint, default is name of view function
- Return: None
- Example:
from hotpot import Hotpot app = Hotpot() @app.route("/") def index(_app,request): return {}
- Note: view function (in the above example, it is the index function) must return a dict object or a Response
-
view_exception_all(self):
- Description: set custom view function for all http exceptions
- Return: None
- Example:
@app.view_exception_all() def view_exception_all(_app,error): if isinstance(error,NOT_FOUND): return JSONResponse({"Not_Found": 404}) return JSONResponse({"": 000})
-
view_exception_404(self):
- Description: set view function for 404 http exception
- Return: None
- Example:
@app.view_exception_404() def view_exception_404(_app,error): return JSONResponse({"HttpException": 404})
Life Circle Hook Decorators:
You can use life circle hook decorators to process app,request, and response. For Example, you may use these hooks to close database connection.
-
after_app(self):
- Description: will call functions decorated by this decorator when app.del is called (or say when free app instance).
- Return: None
- Example:
@app.after_app() def after_app_f01(_app:Hotpot): _app.app_global.db.close()
-
before_request(self):
- Description: will call functions decorated by this decorator before each request is processed.
- Return: None
- Example:
@app.before_request() def before_request(_app: 'Hotpot'): _app.app_global.custom_variable = "hotpot"
-
after_request(self):
- Description: will call functions decorated by this decorator after each request is processed.
- Return: None
- Example:
@app.after_request() def after_request(_app, request) -> Request: print(_app.app_global.custom_variable) return request
- Note: function decorated by this decorator must return an instance of Request
-
before_response(self):
- Description: will call functions decorated by this decorator before each response is processed.
- Return: None
- Example:
@app.before_response() def before_response(_app: 'Hotpot'): _app.app_global.custom_variable = "hotpot"
-
after_response(self):
- Description: will call functions decorated by this decorator after each response is processed.
- Return: None
- Example:
@app.after_response() def after_response(_app, request) -> Response: print(_app.app_global.custom_variable) return response
- Note: function decorated by this decorator must return an instance of Response
Parameters:
- content: content of cookie, e.g. content = {"uid","1"}
- response: response of the request
- security_key: the key to encrypt session data
Return: None
Example:
@app.route("/login")
def login(_app: Hotpot, request: Request):
response = JSONResponse({"Msg": "Successfully Log In!"})
set_session({"name": "hotpot"}, response, security_key: _app.security_key)
return response
Parameters:
- request
- security_key: the key to decrypt session data which must be same as security_key in set_session
Return: cookie data (type: dict)
Example:
@app.route("/user_info")
def user_info(_app: Hotpot, request: Request):
data = get_session(request, _app.security_key)
print(data)
return {}
Parameters:
- response: response of the request
Return: None
Example:
@app.route("/logout")
def logout(_app: Hotpot, request: Request):
clear_session(request)
return {"Msg": "Logout Successful"}
Return: security_key
Example:
app = Hotpot()
app.add_config(
{
"hostname": 'localhost',
"port": 8080,
"debug": True,
"security_key": generate_security_key(),
}
)
app.run()
Parameters:
- location
- code
Return: Response
Description: a simple login util which store uid(user id) in cookie with encryption
Parameters:
- uid: user id which will be stored in cookie with encryption
- response
- security_key: will be used to encrypt data
Return: None
Example:
@app.route("/login")
def login(_app: Hotpot, request: Request):
uid = "1" # get from database, here is just for simple
response = JSONResponse({"Msg": "Successfully Log In!"})
login(uid,response,_app.security_key)
return response
Description: a simple logout util which will clear cookie whose name is 'hotpot'
Parameters:
Return: None
- response
@app.route("/logout")
def logout(_app: Hotpot, request: Request):
response = JSONResponse({"Msg": "Successfully Log In!"})
logout(response)
return response
Description: a simple login required Decorator which check whether there is a 'uid' in cookie
Parameters:
- security_key: used to decrypt data stored in cookie
- fail_redirect: should be an instance of Response, e.g. redirect("/")
Return: None
Example:
@app.route("/user_info")
def user_info(_app: Hotpot, request: Request):
@login_required(security_key=_app.security_key, fail_redirect=redirect("/"))
def wrap(_app: Hotpot, request):
return {"name":"hotpot"}
return wrap(_app, request)