0

I have this code in app.py and it works fine:

from flask import Flask, request, abort, jsonify
from functools import wraps

app = Flask(__name__)

@app.route('/test', methods=['POST'])
def hello():
    return 'Hello'

Now, I added a subdirectory interface under the directory where app.py resides. In this subdirectory I added two files:

__init__.py (empty), and test2.py with the following content:

from flask import Flask, request, abort, jsonify
from functools import wraps

app = Flask(__name__)

@app.route('/test2', methods=['POST'])
def hello2():
    return 'Hello2'

If I try to access /test2 I get a 404 not found error. How to make Flask aware of test2 ?

roganjosh
  • 12,594
  • 4
  • 29
  • 46
ps0604
  • 1,227
  • 23
  • 133
  • 330
  • Going a bit broader, it looks like you're trying to implement the "blueprint" model. This is the source code for the Stack Overflow community website for Python: https://github.com/sopython/sopython-site Is that kind of structure that you're looking to implement? Note how each page has its own set of `routes`, `models` and `forms`. The accompanying site is [here](https://sopython.com/) – roganjosh Feb 08 '19 at 16:56
  • [This is the simplest](https://spin.atomicobject.com/2018/10/11/refactoring-flask-blueprints/) example I found on how to separate Flask functions in multiple files using Blueprint – ps0604 Feb 08 '19 at 17:38
  • As others have stated, blueprints are way to go. But, to make that 'subdirectory' aware: just import that app (do not declare a new one!) instance from the root folder, and declare your route like you did. – no use for a name Feb 09 '19 at 00:28
  • yes, I imported the app when I followed the example above. – ps0604 Feb 09 '19 at 16:08

0 Answers0