Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 698 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting a Flask app into multiple files

#1
I am playing around with flask. My directory structure is like so:

|--------flask-test
|----------------app
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py

The `venv` folder holds my virtual flask installation. My `__init__.py` is like so:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()

This works with no issues however now I would like to split this into two files `__init__.py` and `main.py`. The `main.py` would be in the same directory as `__init__.py`. So far I have tried:

`__init__.py`:

from flask import Flask

app = Flask(__name__)
import main

`main.py`:

from app import app
@app.route("/")
def hello():
return "Hello World!"


However I am getting an error that the module `app` does not exist in `main.py` then I try to run `__init__.py`. What am I doing wrong here? Also where should I put the run method?
Reply

#2
I modified the example a bit, but just want to keep it as simple as possible. Structure looks like this:

app/
__init__.py # empty this time
main.py
run.py

In run you init the app, load the routes from main and putting app.run into \_\_name\_\_ == '\_\_main\_\_' does the trick - because all the code before gets executed.

# in run.py
from flask import Flask
app = Flask(__name__)
from main import *


if __name__ == '__main__':
print app.url_map
app.run(debug=True)

Specify your routes in main.py and import from before starting the app.

# in main.py
from run import app

@app.route("/")
def hello():
return "Hello World!"

Reply

#3
Using **Flask blueprints**, you can break up your application in multiple files which can be put back together into a larger application.

Below is a sample application broken into 3 files that demonstrates just that:
- **file1** : define a */hello* route
- **file2** : define a */world* route
- **file3** : main app, acts as a glue by using register_blueprint function to integrate the apps in file1 and file2

**file1:** defines app_file1 with route /hello

from flask import Blueprint, render_template, session,abort

app_file1 = Blueprint('app_file1',__name__)
@app_file1.route("/hello")
def hello():
return "Hello World from app 1!"

**file2:** defines app_file2 with route /world

from flask import Blueprint, render_template, session,abort

app_file2 = Blueprint('app_file2',__name__)
@app_file2.route("/world")
def world():
return "Hello World from app 2!"

**file3:** main app

from flask import Flask
from file1 import app_file1
from file2 import app_file2
main_app = Flask(__name__)
main_app.register_blueprint(app_file1)
main_app.register_blueprint(app_file2)

**Note**: To simplify things, for this example are kept in a single directory. In a larger application, you would want to further break things into directories/modules. Using modules is extra step allows to only expose the functionality you want public, do extra initialization and avoid circular dependencies.

It is also good practice to have additional folders for things like data access like sqlalchemy models. It's always good to encapsulate files that have common functionality like a payment processing into a module for potential reuse, code clarity and maintenance

If your application has a large amount of views, you may want to have additional subfolders as well
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through