kobinpy / kobin Goto Github PK
View Code? Open in Web Editor NEWType Hints friendly web framework for Python3.6+
Home Page: https://kobin.readthedocs.io/en/latest/
License: MIT License
Type Hints friendly web framework for Python3.6+
Home Page: https://kobin.readthedocs.io/en/latest/
License: MIT License
I want to offer before/after request hooks like this:
from kobin import Kobin, request
app = Kobin()
@app.before_request
def before_request():
print('Before request hook')
But it's just an idea. Your opinion are required.
In production, we use a nginx to return static files (ex. css, images, ... and so on).
But, in development, We want to use a static file server.
In #15 , Add BASE_DIR
, TEMPLATE_DIRS
, and so on.
But these implementations are not great. So REFACTORING it!
combine async/await syntax.
In the following sample code, There is duplicate between types of routing and type hints because\w+
means str
.
@app.route('^/user/(?P<name>\w+)/$')
def hello(name: str):
return "Hello {}".format(name)
So I'd like to write like the following code.
@app.route('^/user/<name>/$')
def hello(name: str):
return "Hello {}".format(name)
In a bottle framework, the documentation says "using a Beaker".
$ make gettext
$ sphinx-intl update -p build/locale
# edit doc/source/locale/*.po files
$ make -e SPHINXOPTS="-D language='ja'" html
text/json
, return the error status and message in json style.text/html
, return the error status and message in html.for improving reusability.
refs.
$ curl -d '{"key":"value"}' -X POST http://127.0.0.1:8000/user/
User Created
$ curl -X POST http://127.0.0.1:8000/user/
User Created
$ curl -d '{"key":"value"}' -X POST http://127.0.0.1:8000/user/
User Created
$ curl -X POST http://127.0.0.1:8000/user/
A server error occurred. Please contact the administrator.
Replace threading.local
with Context Variables that will be added from Python 3.7.
https://www.python.org/dev/peps/pep-0567/
To use werkzeug debugger.
I want to exploit type hints for converting query parameters.
from kobin import Kobin, Response, Param
app = Kobin()
@app.route('/tasks/{task_id}')
def index(task_id: int, query_done: Param[bool]) -> Response:
return Response('Hello World')
from kobin import Kobin, Response, query_param
app = Kobin()
@app.route('/tasks/{task_id}')
@query_param('query_done')
def index(task_id: int, query_done: bool) -> Response:
return Response('Hello World')
A URL Dispatch with regex has high flexibility. But, In the most of the case, It isn't need.
So I suggest matcha like url dispatch with type hints.
It is a simple both the web framework developer and users.
@app.route('/post/{post_id}')
def post_detail(post_id: int):
assert isinstance(post_id, int)
@app.route('/post/{post_id}', converter=lambda id: Post.objects.get(id))
def post_detail(post: Post):
template('post_detail.html', post=post)
Now, kobin always uses kobin.environs.request
as a request. So we can use only LocalRequest
.
Similarly, We can use only LocalResponse
as a response.
However, I want to use a custom request/response like this:
import yaml
from kobin import Kobin
from kobin.environs import LocalRequest
from .environs import MyRequest, MyResponse
app = Kobin()
app.config['REQUEST_CLASS'] = MyRequest
app.config['RESPONSE_CLASS'] = MyResponse
NOTE: It's a just idea. Probably, there are better ways.
Because, it requires in development only. And it is not match new routing system.
Use wsgi-static-middleware
Because it is not compatible with wsgi middleware.
Because, it can offer previous function like this:
$ kobin myapp:app --server wsgiref --port 8000 --staticroot /static --staticdirs
default:
server
: wsgirefport
: 8080staticroot
: `app.config['STATIC_ROOT']staticdirs
: `app.config['STATICFILES_DIRS']secret
module is added by Python3.6
Implement a template adapter for jinja2.
Hmmm...
$ kobin-manager startproject myproject
$ python manage.py runserver # Use conf.py on project root.
$ python manage.py runserver --settings=settings.local
$ python manage.py runserver --settings=settings.prod
view
should has only function that receives request and returns response object. But now, view return bytes
or str
object (ref. https://github.com/c-bata/kobin/blob/master/kobin/app.py#L42 ).
In this framework, the supported python version is 3.5 only.
So we should use new-style classes.
class Hoge:
pass
refs #66
@app.before_request
def before1():
pass
@app.before_request
def before2():
pass
@app.before_request
def before3():
pass
https://docs.python.org/3/library/wsgiref.html#wsgiref.headers.Headers
>>> from wsgiref import headers
>>> headers.Headers
<class 'wsgiref.headers.Headers'>
>>> headers.Headers()
Headers([])
>>> h = headers.Headers()
>>> h.add_header('content-type', 'text/html')
>>> h
Headers([('content-type', 'text/html')])
Sometimes, we want to set query_parameters when redirecting like:
Caution
This option bumped with url variables.
@app.route('/foo/{next}')
def foo(next: str) -> Response:
return Response('foo')
http://mypy-lang.blogspot.jp/2016/05/mypy-04-released.html
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.