Skip to content

RutaTang/Hotpot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hotpot

目前处于开发阶段,更新过快,文档与实际框架内容有出入,暂勿使用

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 ~ _~

Future Goal

Hopefully, Hotpot will be designed as a fast RESTful web framework.

Progress

Hotpot is still in the development stage.

Translation

中文简体: 还没开始写。。欢迎来帮忙!

Installing

install and update hotpot using pip:

pip install hotpot

A Simple App

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.

Tutorial

In this tutorial, we will build a simple poll web.

Document

Note: Only list a few useful and important API which might be enough to build a simple app.

hotpot.app

hotpot.app.Hotpot(main_app=True,name="",base_rule="/")

Parameters:

  1. 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.
  2. name: the name of the app, which is used for namespace of endpoints or view functions.
  3. base_rule: default is "/", app's route rules will be built on base_rule.

Functions:

  1. 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
  2. run(self)

    • Description: simple WSGI Server for development other than production.
    • Return: None
  3. 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:

  1. 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
  2. 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})
  3. 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.

  1. 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()
  2. 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"
  3. 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
  4. 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"
  5. 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

hotpot.sessions

hotpot.sessions.set_session(content: dict, response: ResponseBase, security_key: bytes)

Parameters:

  1. content: content of cookie, e.g. content = {"uid","1"}
  2. response: response of the request
  3. 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

hotpot.sessions.get_session(request: RequestBase, security_key: bytes)

Parameters:

  1. request
  2. 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 {}

hotpot.sessions.clear_session(response: ResponseBase)

Parameters:

  1. response: response of the request

Return: None

Example:

@app.route("/logout")
def logout(_app: Hotpot, request: Request):
    clear_session(request)
    return {"Msg": "Logout Successful"}

hotpot.utils

hotpot.utils.generate_security_key():

Return: security_key

Example:

app = Hotpot()
app.add_config(
    {
        "hostname": 'localhost',
        "port": 8080,
        "debug": True,
        "security_key": generate_security_key(),
    }
)
app.run()

hotpot.utils.redirect(location, code=302)

Parameters:

  1. location
  2. code

Return: Response

hotpot.utils.login(uid: str, response: ResponseBase, security_key: bytes)

Description: a simple login util which store uid(user id) in cookie with encryption

Parameters:

  1. uid: user id which will be stored in cookie with encryption
  2. response
  3. 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

hotpot.utils.logout(response: ResponseBase)

Description: a simple logout util which will clear cookie whose name is 'hotpot'

Parameters:

Return: None

  1. response
@app.route("/logout")
def logout(_app: Hotpot, request: Request):
    response = JSONResponse({"Msg": "Successfully Log In!"})
    logout(response)
    return response

login_required(security_key: bytes, fail_redirect: ResponseBase)

Description: a simple login required Decorator which check whether there is a 'uid' in cookie

Parameters:

  1. security_key: used to decrypt data stored in cookie
  2. 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)

About

A tiny and easy python web framework just for your fun! :)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages