Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

a.k.a. « Flask on steroids »

Project description

https://raw.githubusercontent.com/numberly/flask-stupe/master/artwork/stupeflask.png

https://readthedocs.org/projects/flask-stupe/badge

a.k.a. « Flask on steroids »

An opinionated Flask extension designed by and for web developers to reduce boilerplate code when working with Marshmallow, MongoDB and/or JSON.

Documentation: https://flask-stupe.readthedocs.io

Features

  • Return any object type in views, and it will be coerced to a flask.Response
  • Validate payloads through marshmallow schemas
  • Easily add JSON converters for any custom type
  • Fetch all the blueprints from a whole module in one line
  • Native ObjectId support
  • Powerful configuration management
  • Decorators to handle authentication, permissions, and pagination
  • 100% coverage and no dependency

Comparison

Here is a comparison of a bare Flask application and its equivalent Stupeflask version. They both rely on MongoDB, handle input and output in JSON, and allow to create a user and retrieve one or more.

Bare Flask With Stupeflask
from bson import ObjectId
from flask import abort, Flask, jsonify, request
from marshmallow import Schema
from marshmallow.fields import String
from pymongo import MongoClient

app = Flask(__name__)
users = MongoClient().database.users


class UserSchema(Schema):
    username = String(required=True)
    password = String()


@app.route("/user", methods=["POST"])
def post_user():
    json = request.get_json(force=True)
    validation_result = UserSchema().load(json)
    if validation_result.errors:
        abort(400, validation_result.errors)
    result = users.insert_one(validation_result.data)
    inserted_id = str(result.inserted_id)
    validation_result.data.update(_id=inserted_id)
    return jsonify(validation_result.data)


@app.route("/user/<id>")
def get_user(id):
    try:
        id = ObjectId(id)
    except ValueError:
        abort(404)
    user = users.find_one({"_id": id})
    user["_id"] = str(user["_id"])
    return jsonify(user)


@app.route("/users")
def get_users():
    limit = request.args.get("limit", 100, type=int)
    skip = request.args.get("skip", 0, type=int)
    cursor = users.find().limit(limit).skip(skip)
    return jsonify(list(cursor))
from flask import request
from flask_stupe import paginate, schema_required
from flask_stupe.json import Stupeflask
from marshmallow import Schema
from marshmallow.fields import String
from pymongo import MongoClient

app = Stupeflask(__name__)
users = MongoClient().database.users


class UserSchema(Schema):
    username = String(required=True)
    password = String()


@app.route("/user", methods=["POST"])
@schema_required(UserSchema)
def post_user():
    result = users.insert_one(request.schema)
    request.schema.update(_id=result.inserted_id)
    return request.schema


@app.route("/user/<ObjectId:id>")
def get_user(id):
    return users.find_one({"_id": id})


@app.route("/users")
@paginate(limit=100)
def get_users():
    return users.find()

Project details


Release history Release notifications

This version
History Node

3.6.4

History Node

3.6.3

History Node

3.6.2

History Node

3.6.1

History Node

3.6.0

History Node

3.5.2

History Node

3.5.1

History Node

3.5.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
Flask_Stupe-3.6.4-py2.py3-none-any.whl (15.4 kB) Copy SHA256 hash SHA256 Wheel 3.4 Apr 24, 2018
Flask-Stupe-3.6.4.tar.gz (11.9 kB) Copy SHA256 hash SHA256 Source None Apr 24, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page