Skip to main content

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')

Metadata

Release files for Flask-SimpleView 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-SimpleView 1.0.0
File Size Uploaded Provenance
Flask-SimpleView-1.0.0.tar.gz 3.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-SimpleView 1.0.0
File Interpreter ABI Platform
Flask_SimpleView-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / Flask-SimpleView-1.0.0.tar.gz

Download URL Flask-SimpleView-1.0.0.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ca6afdcb25868b740438a98be6a5a0aefbadc92172eb72086ed446ce4a072fa1
BLAKE2b-256 checksum
How to use checksums
3ee2e2cb564c421e69ba24586c3c39f3545d5ee52cb6d0333ad2aa73016bd368
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / Flask_SimpleView-1.0.0-py3-none-any.whl

Download URL Flask_SimpleView-1.0.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
65a4b188d20b385853abf919ecf730b0d0988fd274c7e5dfffebf02506dec57d
BLAKE2b-256 checksum
How to use checksums
fc2bab0403bdcf10bde914e5c7d66fef424968a6ddc5cf75c27a6ece2062357d
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for Flask-SimpleView 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page