Files
- Directory Structure
- main.py
- main.py
- word_file_app.py
- word_file_app.py – library import
- word_file_app.py – create_application
Directory Structure

main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# main.py # system library -------------------------------------------------------------- import os, sys # local library -------------------------------------------------------------- from config.flask_app import FlaskOptions import word_file_app def main(argv:list) -> None: web_port = FlaskOptions.API_PORT_PROD.value # create the flask app app = word_file_app.create_application( app_name=__name__ ) app.run(host='127.0.0.1', port=web_port,debug=True) if __name__ == '__main__': main(sys.argv[1:]) |
Import some standard libraries, os and sys. They will be used later.
Next import our configuration file for the flask app, and our word_file_app which will talk about below.
Because we are just running the app locally we will set it to 127.0.0.1, in the config file you can set the port you would like to run on, I happen to use 7002.
word_file_app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# /word_merge_app.py # system library ------------------------------------------------------------- import os # packages ------------------------------------------------------------------- from flask import Flask, send_from_directory, render_template, request from flask import send_file, redirect, url_for, Blueprint from flask import session from werkzeug.utils import secure_filename # local library -------------------------------------------------------------- import config.app_config as app_config # routes --------------------------------------------------------------------- from routes.main_app import base_blueprint from routes.maintain_file import maintain_blueprint from routes.merge_file import merge_blueprint from routes.generate_file import generate_blueprint # from routes.processing import processing_blueprint def create_application(app_name): app = Flask(app_name) app.config.from_object(app_config) app.secret_key ='RandomSecretKey' # register blueprints app.register_blueprint(base_blueprint) app.register_blueprint(maintain_blueprint) app.register_blueprint(merge_blueprint) app.register_blueprint(generate_blueprint) @app.context_processor def inject_title(): path = request.path.strip('/') title = path.capitalize() if path else 'Word Document Merge' return dict(title=title) # ------------------------------------------------------------------------ # return the app # ------------------------------------------------------------------------ return app |
Ok, now we will set up the flask app. We will import some standard items from the flask library, next our configuration file for this app, and lastly the different route files to be used in the flask blueprints. We will review all of those later on.
Now let’s go line by line.
word_file_app.py – library import
1 2 3 4 5 6 7 8 |
# system library ------------------------------------------------------------- import os # packages ------------------------------------------------------------------- from flask import Flask, send_from_directory, render_template, request from flask import send_file, redirect, url_for, Blueprint from flask import session from werkzeug.utils import secure_filename |
Importing OS standard library. Next from flask package we are importing the main Flask web handler, functions for sending files, functions for redirect webpages, blueprints, and the all important session.
1 2 |
# local library -------------------------------------------------------------- import config.app_config as app_config |
The next section imports the configuration file for the app, we will cover the settings in the configuration file later on.
1 2 3 4 5 |
# routes --------------------------------------------------------------------- from routes.main_app import base_blueprint from routes.maintain_file import maintain_blueprint from routes.merge_file import merge_blueprint from routes.generate_file import generate_blueprint |
We are going to use four different route files. The reason being is that there really are four separate steps in creating the merged word document. Separating out into different blueprints allows us to keep the logic a little more contained.
routes.main_app, routes.maintain_file, routes.merge_file, routes.generate_file
word_file_app.py – create_application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def create_application(app_name): app = Flask(app_name) app.config.from_object(app_config) app.secret_key ='RandomSecretKey' # register blueprints app.register_blueprint(base_blueprint) app.register_blueprint(maintain_blueprint) app.register_blueprint(merge_blueprint) app.register_blueprint(generate_blueprint) @app.context_processor def inject_title(): path = request.path.strip('/') title = path.capitalize() if path else 'Word Document Merge' return dict(title=title) # ------------------------------------------------------------------------ # return the app # ------------------------------------------------------------------------ return app |
In the main create_application function, we are doing the normal thing you would expect, we are creating the flask application, named app. The function is called from main.py
Next we read in the app_config file, and set the application configuration settings. We don’t have many configuration settings, but allows for future enahancements.
Next we register the blueprints.
routes.main_app, routes.maintain_file, routes.merge_file, routes.generate_file
This allows for all the different endpoints to be available to the application.
1 2 3 4 5 |
@app.context_processor def inject_title(): path = request.path.strip('/') title = path.capitalize() if path else 'Word Document Merge' return dict(title=title) |
The inject_title function is a shortcut way of adding the page title to each of the html template pages we will use later on.
And lastly we return the app to the main.py.
Next we will go through the configuration pages, and how they are used.
Leave a Reply