You can use the mvc pattern in your flask application using this extension.

Overview

GitHub code size in bytes GitHub Workflow Status GitHub PyPI - Downloads PyPI - Python Version PyPI

You can use the mvc pattern in your flask application using this extension.

Installation

Run the follow command to install mvc_flask:

$ pip install mvc_flask

Configuration

To configure the mvc_flask you need import and register in your application:

from mvc_flask import FlaskMVC
mvc = FlaskMVC()

Or use factory function

mvc = FlaskMVC()

def create_app():
  ...
  mvc.init_app(app)

By default the mvc_flask assumes that your application directory will be app, but, you can change it. Passing the object of configuration:

app.config["FLASK_MVC_DIR"] = "sample_app"

Create MVC Pattern

mvc_flask assumes that your application will have these characteristics:

app
├── __ini__.py
├── controllers
│   └── home_controller.py
├── models
├── routes.json
└── views
    ├── index.html

The routes.json file should be like this:

[
  {
    "method": "GET",
    "path": "/",
    "controller": "home",
    "action": "index"
  },
]

The home_controller.py file should be like this:

from flask.templating import render_template

class HomeController:
    def index(self):
        return render_template("index.html")

Tests

You can run the tests, executing the follow command:

$ make test

Comments
  • jinja2.exceptions.TemplateNotFound: index.html

    jinja2.exceptions.TemplateNotFound: index.html

    Hi I cloned your code and while trying to use the example app I am getting this error

    raise TemplateNotFound(template)
    

    jinja2.exceptions.TemplateNotFound: index.html

    I tried creating a templates folder under the example directory and copied contents from the view folder to it. still the same error

    Appreciate your help

    bug question 
    opened by bjenmonk 8
  • FlaskMVC reinitialize app.template_folder and drop value settings in Flask initialize

    FlaskMVC reinitialize app.template_folder and drop value settings in Flask initialize

    This is cool mvc implementation, especially for me.

    When i deep to Flask, i found the mvc-flask. It so like for me, because i'd RoR programmer. So, i have to implement my folder structure identical to rails application. But i found two incompatibilities with Ruby on Rails.

    1. I can't set folder for templates, when initialize Flask:
    app = Flask(self.__name, template_folder = './app/views')
    ...
    FlaskMVC(app, path = 'app')
    

    After that, Flask searches templates in ./views folder, but not in ./app/views as i want. So, i try to propose pull-request to fix this behaviour. With best regards to maintainers. FlaskMVC making python better (for me, for example).

    question 
    opened by bsa7 1
  • NameError: name 'view' is not defined

    NameError: name 'view' is not defined

    Hi, I have recently started working with mvc-flask, however, I unfortunately cannot seem to get view() to work. (view is undefined)

    app/routes.py

    from mvc_flask import Router
    
    Router.get("/", "landing#route")
    

    app/controllers/landing_controller.py

    from flask import session
    
    class LandingController:
        def route(self):
            return view("index.html")
    

    app.py

    from flask import Flask
    from mvc_flask import FlaskMVC
    
    app = Flask(__name__)
    FlaskMVC(app)
    
    doc question 
    opened by fzorb 1
  • do we improve routes using yml?

    do we improve routes using yml?

    What do you think about changing the definition of routes from json to yaml?

    Something like:

    routes:
      home:
        index
      
      books:
        new:
          path: /
        create:
          path: /create
          method: post
      
      posts:
        models: true
        only:
          - show
          - new
          - create
      
      users:
        index:
          hooks:
            before_request: "required"
    

    if the user define model: true the mvc-flask automatically will be create the follows routes:

    - index (/)
    - show (/show/:id)
    - new (/new)
    - create (/create)
    - edit (/edit/:id)
    - update (/)
    - delete (/delete/:id)
    

    But, you can use the only parameter to regular this.

    enhancement question 
    opened by marcuxyz 1
  • [CI/CD] change requirements.txt to poetry

    [CI/CD] change requirements.txt to poetry

    Currently, we use pip install requirements.txt inside GitHub action to install all dependencies on the project. We should support poetry directly instead of export the requirements files.

    opened by marcuxyz 1
  • Sent to data through form[method=put]

    Sent to data through form[method=put]

    Problem

    The HTML default not work with put and delete. Then... We need sent the form data to receive in update route. Recently not sent, because need javascript called.

    Solution

    Create javascript helper that will be called every time that the request form has been sent

    Comments

    We need follow the solutions of greats frameworks, as: Laravel, Rails and etc.

    Screenshot 2022-10-31 at 16 20 30 enhancement 
    opened by marcuxyz 0
  • create namespace or group method

    create namespace or group method

    Recently we have not group defined to routes, the routes can work as prefix-url of many routes. I haven't thought of a definitive model, but, it can be:

    g = Router.namespace("/tasks") 
    
    # or
    
    g = Router.group("/tasks")
    
    g.get("/", "tasks#index")  # you can access: https://.../tasks/
    
    opened by marcuxyz 0
  • action could receive view and request object

    action could receive view and request object

    currently we have import render_template function to use templates and request to accomplish operation based in visitord request.

    Would cool if these objects was available. e.g:

    class HomeController:
        def index(self, view, request):
            return view("index.html")
    
    enhancement 
    opened by marcuxyz 0
  • create  al method to register all routes

    create al method to register all routes

    You can use Router.all() to register all routes of CRUD.

    Router.all("users")
    

    The previous command produce this:

    users.create     POST     /users
    users.delete     DELETE   /users/<id>
    users.edit       GET      /users/<id>/edit
    users.index      GET      /users
    users.new        GET      /users/new
    users.show       GET      /users/<id>
    users.update     PUT      /users/<id>
    

    You can also use only parameter to control routes, e.g:

    Router.all("messages", only="index show new create")
    

    The previous command produce this:

    messages.create  POST     /messages
    messages.index   GET      /messages
    messages.new     GET      /messages/new
    messages.show    GET      /messages/<id>
    

    The paramenter only accept string or array, so, you can use only=["index", "show", "new", "create"]

    close #16

    enhancement 
    opened by marcuxyz 0
  • update mvc_flask to 2.0.0

    update mvc_flask to 2.0.0

    This verion contain break change:

    • Now the routes are be registered using .py file.
    • To standardize the application the mvc_flask extension just work with app directory
    • You can register routes based in methods: GET, POST, UPDATE and DELETE
    • we no longer support to routes.json file.

    To register routes you can use the routes.py inside app directory and the file must contain the import Router object, The Router object must be used to register the routes. You can use GET, POST, UPDATE and DELETE methods to register routes. E.g:

    from mvc_flask import Router
    
    Router.get("/", "home#index")
    Router.get("/hello", "home#hello")
    Router.post("/messages", "messages#create")
    Router.put("/users/<id>", "users#update")
    Router.delete("/users/<id>", "users#delete")
    
    opened by marcuxyz 0
  • Refactory FlaskMVC class

    Refactory FlaskMVC class

    It would be nice if we could refactor the FlaskMVC class including the blueprint calls to create the new routes.

    This will make it easier to call hooks, such as:

    • before_requests
    • after_requests

    and etc. Related by issue #10

    refactory 
    opened by marcuxyz 0
  • Create CLI commands for mvc

    Create CLI commands for mvc

    For enhancement the extensions, we could create commands for generate controllers and etc. E.g:

    flask generate controller home

    The command must generate file:

    app
    ├── controllers
    │   └── home_controller.py
    
    enhancement 
    opened by marcuxyz 0
Releases(2.6.0)
  • 2.4.0(Aug 8, 2022)

    • Include patch HTTP verb in update route.
    • Parameters ~~view~~, ~~request~~ has been removed
    class HomeController:
        def index(self, view, request):
            return view("index.html")
    

    Now, you can import render_template and request directly of flask package.

    from flask import render_template
    
    class HomeController:
        def index(self):
            return render_template("index.html")
    
    • Enhancement of unit tests
    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(Nov 11, 2021)

    You can use Router.all() to register all routes of CRUD.

    Router.all("users")
    

    The previous command produce this:

    users.create     POST     /users
    users.delete     DELETE   /users/<id>
    users.edit       GET      /users/<id>/edit
    users.index      GET      /users
    users.new        GET      /users/new
    users.show       GET      /users/<id>
    users.update     PUT      /users/<id>
    

    You can also use only parameter to controll routes, e.g:

    Router.all("messages", only="index show new create")
    

    The previous command produce this:

    messages.create  POST     /messages
    messages.index   GET      /messages
    messages.new     GET      /messages/new
    messages.show    GET      /messages/<id>
    

    The paramenter only accept string or array, so, you can use only=["index", "show", "new", "create"]

    Source code(tar.gz)
    Source code(zip)
  • 2.0.0(Nov 1, 2021)

    This verion contain break change:

    • Now the routes are be registered using .py file.
    • To standardize the application the mvc_flask extension just work with app directory
    • You can register routes based in methods: GET, POST, UPDATE and DELETE
    • we no longer support to routes.json file.

    To register routes you can use the routes.py inside app directory and the file must contain the import Router object, The Router object must be used to register the routes. You can use GET, POST, UPDATE and DELETE methods to register routes. E.g:

    from mvc_flask import Router
    
    Router.get("/", "home#index")
    Router.get("/hello", "home#hello")
    Router.post("/messages", "messages#create")
    Router.put("/users/<id>", "users#update")
    Router.delete("/users/<id>", "users#delete")
    
    Source code(tar.gz)
    Source code(zip)
Owner
Marcus Pereira
Cristão, amante de jogos eletrônicos e desenvolvimento de software.
Marcus Pereira
Microservice example with Python, Faust-Streaming and Kafka (Redpanda)

Microservices Orchestration with Python, Faust-Streaming and Kafka (Redpanda) Example project for PythonBenin meetup. It demonstrates how to use Faust

Lé 3 Jun 13, 2022
Klein - A micro-framework for developing production-ready web services with Python

Klein, a Web Micro-Framework Klein is a micro-framework for developing production-ready web services with Python. It is 'micro' in that it has an incr

Twisted Matrix Labs 814 Jan 08, 2023
Endpoints is a lightweight REST api framework written in python and used in multiple production systems that handle millions of requests daily.

Endpoints Quickest API builder in the West! Endpoints is a lightweight REST api framework written in python and used in multiple production systems th

Jay Marcyes 30 Mar 05, 2022
Free and open source full-stack enterprise framework for agile development of secure database-driven web-based applications, written and programmable in Python.

Readme web2py is a free open source full-stack framework for rapid development of fast, scalable, secure and portable database-driven web-based applic

2k Dec 31, 2022
Library for building WebSocket servers and clients in Python

What is websockets? websockets is a library for building WebSocket servers and clients in Python with a focus on correctness and simplicity. Built on

Aymeric Augustin 4.3k Dec 31, 2022
A comprehensive reference for all topics related to building and maintaining microservices

This pandect (πανδέκτης is Ancient Greek for encyclopedia) was created to help you find and understand almost anything related to Microservices that i

Ivan Bilan 64 Dec 09, 2022
Try to create a python mircoservice framework.

Micro current_status: prototype. ... Python microservice framework. More in Document. You should clone this project and run inv docs. Install Not now.

修昊 1 Dec 07, 2021
Djask is a web framework for python which stands on the top of Flask and will be as powerful as Django.

Djask is a web framework for python which stands on the top of Flask and will be as powerful as Django.

Andy Zhou 27 Sep 08, 2022
Asynchronous HTTP client/server framework for asyncio and Python

Async http client/server framework Key Features Supports both client and server side of HTTP protocol. Supports both client and server Web-Sockets out

aio-libs 13.2k Jan 05, 2023
A framework that let's you compose websites in Python with ease!

Perry Perry = A framework that let's you compose websites in Python with ease! Perry works similar to Qt and Flutter, allowing you to create componen

Linkus 13 Oct 09, 2022
A high-level framework for building GitHub applications in Python.

A high-level framework for building GitHub applications in Python. Core Features Async Proper ratelimit handling Handles interactions for you (

Vish M 3 Apr 12, 2022
The source code to the Midnight project

MidnightSniper Started: 24/08/2021 Ended: 24/10/2021 What? This is the source code to a project developed to snipe minecraft names Why release? The ad

Kami 2 Dec 03, 2021
Django Ninja - Fast Django REST Framework

Django Ninja is a web framework for building APIs with Django and Python 3.6+ type hints.

Vitaliy Kucheryaviy 3.8k Jan 02, 2023
Online Boutique is a cloud-native microservices demo application

Online Boutique is a cloud-native microservices demo application. Online Boutique consists of a 10-tier microservices application. The application is

Matt Reider 1 Oct 22, 2021
Async Python 3.6+ web server/framework | Build fast. Run fast.

Sanic | Build fast. Run fast. Build Docs Package Support Stats Sanic is a Python 3.6+ web server and web framework that's written to go fast. It allow

Sanic Community Organization 16.7k Jan 08, 2023
Bablyon 🐍 A small ASGI web framework

A small ASGI web framework that you can make asynchronous web applications using uvicorn with using few lines of code

xArty 8 Dec 07, 2021
JustPy is an object-oriented, component based, high-level Python Web Framework

JustPy Docs and Tutorials Introduction JustPy is an object-oriented, component based, high-level Python Web Framework that requires no front-en

927 Jan 08, 2023
WebSocket and WAMP in Python for Twisted and asyncio

Autobahn|Python WebSocket & WAMP for Python on Twisted and asyncio. Quick Links: Source Code - Documentation - WebSocket Examples - WAMP Examples Comm

Crossbar.io 2.4k Jan 06, 2023
Loan qualifier app - Loan Qualifier Application Built With Python

Loan Qualifier Application This program is designed to automate the discovery pr

Phil Hills 1 Jan 04, 2022
Trame let you weave various components and technologies into a Web Application solely written in Python.

Trame Trame aims to be a framework for building interactive applications using a web front-end in plain Python. Such applications can be used locally

Kitware, Inc. 85 Dec 29, 2022