[Python] Split a large Flask file using Blueprint

Introduction

When creating a web application using Flask, which is a web framework of python, the number of lines in the Flask file tends to increase, so I considered file division. It seems that file splitting using Brueprint is common in Flask, and this time I used Blueprint to split the file.

Directory structure

├── main.py
├── COMP_A
│   └── func_A.py
├── COMP_B
│   └── func_B.py
└── templates
    ├── COMP_A
    │   ├── index_A_1.html
    │   └── index_A_2.html
    └── COMP_B
        ├── index_B_1.html
        └── index_B_2.html

The main program is main.py. In addition, it is assumed that there are components A and B. Func_A.py and func_B.py in each component are files that use Flask respectively.

In flask, the html file is stored under the template, but this time, a directory for each component was prepared under the template and the html file was stored.

Source code

func_A.py

from flask import render_template
from flask import Blueprint

bpa = Blueprint('bpa', __name__, url_prefix='/A')

@bpa.route('/a1')
def app_a1():
    return render_template('COMP_A/index_A_1.html')
    #return "hello A a1"

@bpa.route('/a2')
def app_a2():
    return render_template('COMP_A/index_A_2.html')
    #return "hello A a2"

func_B.py


from flask import render_template
from flask import Blueprint

bpb = Blueprint('bpb', __name__, url_prefix='/B')

@bpb.route('/b1')
def app_b1():
    return render_template('COMP_B/index_B_1.html')
    #return "hello B b1"

@bpb.route('/b2')
def app_b2():
    return render_template('COMP_B/index_B_2.html')
    #return "hello B b2"

main.py

from flask import Flask

app = Flask(__name__)

from COMP_A.func_A import bpa
from COMP_B.func_B import bpb

@app.route('/')
def index():
    return 'Hello main'

app.register_blueprint(bpa)
app.register_blueprint(bpb)

if __name__ == '__main__':
    app.debug = True
    app.run(host='127.0.0.1',port=60000)

Originally, the function is registered for the app generated by Flask (name), but in the case of a function in another file, Brueprint is registered in the app using register_blueprint. At this time, don't forget to import another function Brueprint in advance.

call

Specify the following in the URL

127.0.0.1:60000/      #main.py   index()
127.0.0.1:60000/A/a1  #func_A.py  app_a1()
127.0.0.1:60000/A/a2  #func_A.py  app_a2()
127.0.0.1:60000/B/b1  #func_B.py  app_b1()
127.0.0.1:60000/B/b2  #func_B.py  app_b2()

Note that the URL is a combination of Prefix when Brueprint is specified and the path when @ xxx.route is defined.

Other

--File split using module There seems to be a way to split the file using flask.module. Consider next time.

return redirect(url_for('bpb.b2'))

Recommended Posts

[Python] Split a large Flask file using Blueprint
[Python] Read a csv file with a large data size using a generator
Create a GIF file using Pillow in Python
I tried reading a CSV file using Python
Run a Python file from html using Django
Create a MIDI file in Python using pretty_midi
Try creating a compressed file using Python and zlib
[Python] I tried running a local server using flask
Creating a web application using Flask ②
Creating a wav file split program
I made a Line-bot using Python!
Create a python GUI using tkinter
Drawing a silverstone curve using python
Create a binary file in Python
Creating a web application using Flask ①
Extract the targz file using python
Creating a web application using Flask ③
Creating a web application using Flask ④
[Python] File operation using if statement
Python: Introduction to Flask: Creating a number identification app using MNIST
Process Splunk execution results using Python and save to a file
Creating a simple PowerPoint file with Python
Favicon placement (when using Python, Flask, Heroku)
Impressions of using Flask for a month
[Python] Create a Batch environment using AWS-CDK
Create a large text file with shellscript
Scraping a website using JavaScript in Python
Launch a Flask app in Python Anywhere
Create a deb file from a python package
[Python] Scraping a table using Beautiful Soup
[GPS] Create a kml file in Python
Draw a tree in Python 3 using graphviz
A program that plays rock-paper-scissors using Python
I want to make a web application using React and Python flask
I made a configuration file with Python
[Raspberry Pi] Publish a web application on https using Apache + WSGI + Python Flask
Split mol2 file with python (-> 2016.04.17 Also supports sdf file)
How to read a CSV file with Python 2/3
Read a file containing garbled lines in Python
Try to separate Controllers using Blueprint in Flask
ffmpeg-Build a python environment and split the video
CRLF becomes LF when reading a Python file
How to create a JSON file in Python
Create a web map using Python and GDAL
Create wav file from GLSL shader using python3
View drug reviews using a list in Python
Launch a web server with Python and Flask
A memorandum for touching python Flask on heroku
Export Python3 version OpenCV KeyPoint to a file
Create a Photoshop format file (.psd) with python
Create a Mac app using py2app and Python3! !!
Let's make a module for Python using SWIG
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
Run a python script from excel (using xlwings)
A story when a Python user passes a JSON file
I made a Mattermost bot with Python (+ Flask)
Script python file
Start using Python
Python file processing