A very simple wrapper around flask's MethodView to ease the adding of views to the app or blueprints
Project description
Very simple wrapper around Flask
Install
pip install flask_simpleview
Use
An example for a simple view:
from flask_simpleview import Flask, SimpleView
# this works exactly the same as flask.Flask
# flask_simpleview.Flask is subclassed from flask.Flask
# the only difference is the addition of 2 methods: `add_view` and `add_api`
app = Flask(__name__)
# and the same blurb again:
# this works exactly the same as flask.Blueprint
# flask_simpleview.Blueprint is subclassed from flask.Blueprint
# the only difference is the addition of 2 methods: `add_view` and `add_api`
blueprint = Blueprint('blueprint', __name__)
# as Flask and Blueprint are the same as their parent classes
# this will obviously work
app.register_blueprint(blueprint)
# This works exactly the same as flask.views.MethodView
# flask_simpleview.SimpleView is subclassed from flask.views.MethodView
# the only difference is that you encapsulate the rule (route) and
# the endpoint in the class
class SignUp(SimpleView):
rule = '/sign-up'
endpoint = 'sign_up'
template = 'sign_up.html'
def get(self):
# just assuming a form for the demonstration
form = SignUpForm()
# you don't need to pass the template string, if registered above
return self.render_template(form=form)
def post(self):
form = SignUpForm(request.form)
if form.validate_on_submit():
sign_up_user_from_form(form)
# the SimpleView class has access to all flask functions
# `return self.thing` is the same as `return getattr(flask, 'thing')`
return self.redirect(self.url_for('login'))
else:
return self.render_template(form=form)
app.add_view(SignUp)
blueprint.add_view(SignUp)
With a blueprint:
from flask_simpleview import Blueprint, Login
auth = Blueprint('auth', __name__)
class Login(SimpleView):
rule = '/login'
endpoint = 'login'
template = 'login.html'
def get(self):
return self.render_template()
def post(self):
try:
login_user(request.form)
return self.redirect(self.url_for('app.dashboard'))
except LoginFailed as e:
return self.render_template(errors=e)
auth.add_view(Login)
Or if you want to specify the template in self.render_template:
class SignUp(SimpleView):
rule = '/sign-up'
endpoint = 'sign_up'
def get(self):
return self.render_template('sign_up.html')
No need for views just to have templates:
class LegacyDashboardRedirect(SimpleView):
rule = '/dashboard/v1/home'
endpoint = 'v1_dashboard'
def get(self):
return self.redirect(self.url_for('v2_dashboard'))
Or for apis:
class UsersAPI(SimpleView):
rule = '/api/users'
endpoint = 'users'
def get(self):
user_id = request.args.get('id')
if user_id:
return self.jsonify(db.session.query(User).get(user_id).to_json())
else:
return self.jsonify([u.to_json() for u in db.session.query(User)]
You don't have to use self either, you can use flask of course:
from flask import redirect
class AnotherView(SimpleView):
rule = '/another-view'
endpoint = 'another_view'
def get(self):
return redirect('https://www.example.com')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file Flask-SimpleView-1.0.0.tar.gz.
File metadata
- Download URL: Flask-SimpleView-1.0.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca6afdcb25868b740438a98be6a5a0aefbadc92172eb72086ed446ce4a072fa1
|
|
| MD5 |
129d85ae3bef11b75ad69125438fc522
|
|
| BLAKE2b-256 |
3ee2e2cb564c421e69ba24586c3c39f3545d5ee52cb6d0333ad2aa73016bd368
|
File details
Details for the file Flask_SimpleView-1.0.0-py3-none-any.whl.
File metadata
- Download URL: Flask_SimpleView-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65a4b188d20b385853abf919ecf730b0d0988fd274c7e5dfffebf02506dec57d
|
|
| MD5 |
887e3f7c3261dff2a19c6e235e4ced66
|
|
| BLAKE2b-256 |
fc2bab0403bdcf10bde914e5c7d66fef424968a6ddc5cf75c27a6ece2062357d
|