-2

I struggled with this question for 2 days and has no idea. I have 3 folders containing 3 individual python scripts each folder script run a function.

My question is how can I access them individually?

for only 1 folder, I create, say app.py with all routes inside. but how can I access 3 folder individually and can run individual function?

My file skeleton likes:

app.py (entrance)
|---departmentA
......|-------runme.py
......|-------templates
...........|-----index.html
|---departmentB
......|-------runme.py
......|-------templates
...........|-----index.html
|---departmentC
......|-------runme.py
......|-------templates
...........|-----index.html

Thanks Alex

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
user1042911
  • 91
  • 2
  • 6
  • sorry forget to tell...i am running Flask how to use the route to access 3 different folders? – user1042911 Mar 15 '22 at 12:12
  • 1
    https://docs.python-guide.org/writing/structure/ and [what-is-the-best-project-structure-for-a-python-application](https://stackoverflow.com/questions/193161/what-is-the-best-project-structure-for-a-python-application) and [how-to-divide-flask-app-into-multiple-py-files](https://stackoverflow.com/questions/11994325/how-to-divide-flask-app-into-multiple-py-files) .. plenty of ressources already on SO – Patrick Artner Mar 15 '22 at 12:13
  • [edit] your question with a [mre]. – Patrick Artner Mar 15 '22 at 12:14
  • 1
    You can do this using Blueprints. – Alvin Cruz Mar 15 '22 at 13:16

1 Answers1

-1

File Structure:

app.py
/departmentA
    __init__.py
    routes.py
    runme.py
    /pages/departmentA
        index.html

app.py

from flask import Flask
import departmentA

skill_app = Flask(__name__)
skill_app.register_blueprint(departmentA.bp)

print(departmentA.my_func())

skill_app.run()

/departmentA/init.py

from .routes import bp
from .runme import my_func

/departmentA/routes.py

from flask import Blueprint, render_template
from .runme import my_func

bp = Blueprint('dept_A', __name__, template_folder='pages', url_prefix='/department_A')

@bp.route('/')
def index():
    print(my_func())
    return render_template('departmentA/index.html')

/departmentA/runme.py

def my_func():
    return "Hello World!"

Use same format in other departments.

Alvin Cruz
  • 160
  • 5
  • Hi Thanks I am new to Blueprint. so I copied the same structure and I can type http://127.0.0.1/departmentA/ it will route to departmentA/ and http://127.0.0.1/departmentB/ it will route to departmentB/ right? also the python code will also be run within each department? sorry I am new to Blueprint...need to digest – user1042911 Mar 15 '22 at 15:03
  • Ok I copied and it works to open different HTML. However, inside each html, say HTMLA I need to call for example
    so "/upload" will trigger the route in app.py and in HTMLB, will call action "/sendemail" also will trigger the route in app.py......my question is if I have many pages, then app.py will have many routes to handle different HTML's post requests?
    – user1042911 Mar 15 '22 at 15:30
  • oh i misunderstood and after testing it a bit clear now...everything is route to "routes.py" in each department. – user1042911 Mar 15 '22 at 15:53