Skip to content

Flask Notes


Creating a Flask App Now using Pipenv

  • Create directory and cd into directory
  • pipenv shell
  • Install flask
  • pipevn install flask SQLAlchemy psycopg2 click uwsgi


REST - Serializing and deserialing JSON data

  • Using Marshmallow
    • Declaring Schemas
    • Implicit field creation - let marshmallow figure out the field.
      class DemoSchema(Schema):
        class Meta:
          fields=("name", "email", "created_at", "uppername")
  • Using Marshmallow with jsonschema to validate data from api - No longer recommended. Latest Marsmallow includes actual validation
    • Using marshmallow-jsonschema - marshmallow-jsonschema
      • Use to dump jsonschema from marshmallow
    • Using jsonschema - JsonSchema
      • Uses jsonshema validate, ValidationError
      • Use the jsonschema from marshmallow-jsonschema to validate content from api
            åsch = ReceiptSchema()
            js_schema = json_schema.dump(sch).data
            receipt = request.get_json()
                validate(receipt, js_schema)
                receipt_data = sch.load(receipt).data
            except ValidationError as err:
                resp['err'] = err.message

CORS Configuration - Without using Flask-CORS

Last update: April 13, 2020 15:25:03