Skip to main content

Flask MVC framework

Project description

Introduction

Configuration

The base configuration file is located on the config dir.

SSL Encryption

For enabling ssl encryption within the application, you will need to add in the "SERVER_ENV" key entry in the config file

  SSL:
    Certificate: "path to the cer file (public key)"
    PrivateKey: "path to the pki file (private key)"

You can add default database using only the configuration file.

Default database with builtin driver in sqlalchemy

...
DATABASES: 
  default: mysql
  mysql: 
    driver: mysql+pymysql
    user: "replace this with your database user"
    password: "replace this with your database user's password"
    database: "replace this with your database name"
    address: "replace this with your hostname"
    models: "mysql (python module that require to be put under Models.Persistent module)"
    readonly: false
...

Default database with non builtin driver in sqlalchemy

...
DATABASES:
  informix:
    driver: informix
    user: "replace this with your database user"
    password: "replace this with your database user's password"
    database: "replace this with your database name"
    address: "replace this with your hostname"
    models: "informix (python module that require to be put under Models.Persistent module)"
    params:
      SERVER: "replace with your server name"
      CLIENT_LOCALE: "replace with your client locale"
      DB_LOCALE: "replace with your server locale"
    dialects:
      informix: 
        module: IfxAlchemy.IfxPy
        class: IfxDialect_IfxPy
      informix.IfxPy: 
        module: IfxAlchemy.IfxPy
        class: IfxDialect_IfxPy
      informix.pyodbc: 
        module: IfxAlchemy.pyodbc
        class: IfxDialect_pyodbc
    readonly: false
...

"params" are parameters that need to be send within the connection to the database. In that example using informix database "SERVER", "CLIENT_LOCALE" and "DB_LOCALE" are required parameters for the connection to the database.

"dialects" are the python modules configuration to translate models into sql statements to query the database

By default escape char between url and first param is ? and escape char between parameters is & but they can be changed by adding within your database params section:

...
      url_param_separator: '?' #Change it with yours
      params_separator: '&' #Change it with yours
...

Multiple databases

...
DATABASES:
  db01:
    ...
  db02:
    ...
...

Adding users session

To enable sessions in the server you need to add "APP_KEY" and "SESSION" into the "SERVER" section in the configuration file

"APP_KEY" : random string value (keep that secret)

"SESSION" : string value, possible values are ["filesystem", "memcahed", "redis", "mongodb", "sqlalchemy"]

Using filesystem, redis or memcached based sessions

...
SERVICES:
  redis:
    HOST: localhost
    PORT: 6379
  filesystem:
    PATH: sessions
  memcached:
    HOST: localhost
    PORT: 11211
  mongodb:
    driver: mongodb
    user: "replace this with your database user"
    password: "replace this with your database user's password"
    database: "replace this with your database name"
    address: "replace this with your hostname"
    collection: "replace this with your collection name for the sessions"

Using mongodb or sqlalchemy based sessions

Session based on sqlalchemy will use the default configured database

...
DATABASES:
  default: mysql
  mysql:
    driver: mysql+pymysql
    user: "replace this with your database user"
    password: "replace this with your database user's password"
    database: "replace this with your database name"
    address: "replace this with your hostname"
    models: "mysql (python module that require to be put under Models.Persistent module)"
    readonly: false
...

Adding cors to the server

...
FLASK:
  CONFIG:
    CORS_ORIGINS:
      - "http://localhost"    
    CORS_ALLOW_HEADERS: 
      - Content-Type 
      - Authorization
    CORS_ALWAYS_SEND: true
    CORS_AUTOMATIC_OPTIONS: true
    CORS_EXPOSE_HEADERS: Authorization
    CORS_INTERCEPT_EXCEPTIONS: true
    CORS_MAX_AGE: null
    CORS_METHODS: 
      - GET
      - HEAD
      - POST
      - OPTIONS
    CORS_SEND_WILDCARD: false
    CORS_SUPPORTS_CREDENTIALS: true
    CORS_VARY_HEADER: true
    ...
...

Creating server routes

There are 3 files where you could register your flask server routes, You could find these file under the src/Server folder:

  • Errors:

All the server http error code must be registered inside the init method of the ErrorHandler.py file.

Example:

server.register_error_handler(500, Controllers.Web.HTTP50XController.error500)
  • Web based http file routes:

All the web based http routes must be registered inside the init method of the Web.py file.

Example:

server.add_url_rule('/', 'home', Controllers.Web.HomeController.index, methods=['GET'])
  • Rest api routes:

All the Rest API based routes must be registered inside the init method of the WS.py file.

Example:

server.add_url_rule('/api/', 'api', Controllers.WS.ApiController.index, methods=['GET'])

Creating controllers:

Pro tip, when using database, make sure to use decorator @safe from flask_framework_mvc.Database.decorators over your controllers functions that requires database(s) access. This ensure database is available after a long period on inactivity on the database session.

  • Web based http file controllers:

All web based http file controllers must be placed under the Controllers.Web module.

The class based controllers that you register into the app must be imported into the __init__.py file of the Controller.Web module.

The file based that contain your view functions must must also be inmported into the __init__.py file of the Controller.Web module.

  • Rest api controllers:

All Rest API based controllers must be placed under the src/Controllers/WS folder.

The class based controllers that you register into the app must be imported into the __init__.py file of the Controller.Web module.

The file based that contain your view functions must must also be inmported into the __init__.py file of the Controller.Web module.

Creating models:

you can create SQLAlchemy models by creating a new module under the Models.Persistent module and place each models inside your module that you previously created.

The models that you register into the app must be an Database.Model or Database.get_models_by_name('replace that with your database connection name') object, you could import this object using the following line into your database model:

from Database import Database

All models must be imported inside the __init__.py of your base module and you must import this module in the __init__.py of the Models.Persistent module

Creating scheduling tasks:

Tasks are some python code that are running at specific interval time. These task must be placed inside the src/Task folder. After that you must add these line inside the src/server.py file to enable your task function:

    Server.Process.add_task("Task.YourFileOrClass.YourStaticMethodOrClassMethod", second=30)

Note the task you are registering must be before the line:

    Server.Process.start(args)

Static folder:

The src/static folder contains all static file for your web based application.

Template folder:

The src/template folder contains layouts and templates file for your web based application. Those files are content configurable, you can also import layout inside the your template file, it allow you to have only content editable part into your template file.


Using docker-compose file:

  • First start of the flask server:
docker-compose up 
  • To start the flask server:
docker-compose start 
  • To restart the flask server
docker-compose restart 
  • To shutdown the flask server:
docker-compose stop 

Running on Azure Function App:

# coding: utf-8
import azure.functions as functions
import flask_framework.azure 
import logging
import os


os.environ.setdefault('CONFIG_FILE', './config/config.yml')
app = functions.WsgiFunctionApp(flask_framework.azure.AzureFunctionsApp(), http_auth_level=functions.AuthLevel.ANONYMOUS)
  • make sure to have in your host.json
...
  "extensions": {
    "http": { "routePrefix": ""}
  },
...

Running on local desktop:

We assume that your system already had python v3+ and pip v3+ installed.

  • installation:
git clone https://github.com/frederickney/flask-framework.git
cd flask-framework
pip3 install

or

pip install flask-framework-mvc
  • CLI interface
python -m flask_framework.cli -h
  • Create a new project
python -m flask_framework.cli -cp <your project>

or

python -m flask_framework.cli --create-project <your project>

When the project is created, more command can be used when the env "CONFIG_FILE" is set and can be run through

python -m flask_framework.app

see -h for usages

  • Starting the flask server attached to an ide such as PyCharm

Setup the configuration as seen bellow in the screenshots

Configurations

[!NOTE] "LOG_DIR" and "LOG_FILE" env are no longer mandatory for starting the process

Environment variables

[!WARNING] Issue raised, it is no longer working attached to ide using flask module: link to issue

  • On every startup
export CONFIG_FILE=config/config.yml
  • Starting the flask server in standalone
python -m flask_framework.server
  • Starting the flask server with gunicorn and workers process
python -m flask_framework.wsgi

LICENSE

See License file

Project details


Download files

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

Source Distribution

flask_framework_mvc-1.2.1.tar.gz (114.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_framework_mvc-1.2.1-py3-none-any.whl (118.6 kB view details)

Uploaded Python 3

File details

Details for the file flask_framework_mvc-1.2.1.tar.gz.

File metadata

  • Download URL: flask_framework_mvc-1.2.1.tar.gz
  • Upload date:
  • Size: 114.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flask_framework_mvc-1.2.1.tar.gz
Algorithm Hash digest
SHA256 e2a328085b11d7bf3b49c499e00890992096cb5328555b13514d111e51a571c0
MD5 a498dd68fa6efda863bfee3448c4b187
BLAKE2b-256 1039169a7e567d7cb3e37f329075204a57146530780f642a5b9adb8c2e679602

See more details on using hashes here.

File details

Details for the file flask_framework_mvc-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_framework_mvc-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8c5e84e7051df19c53934954c8f47eb71410440e8f6fd3bd21067368d8190f5e
MD5 de2f7fa3fed8dd39c6a77140ee941133
BLAKE2b-256 c85fff9b80e4201da4bbefa42cc5b380ccf02870991a1fea965935ad8569c585

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page