Reputation: 20150
When using Flask, is it compulsory to have all method definition in one file, because i'm trying to move some methods definitions in another file but 404 not found error.
Upvotes: 2
Views: 693
Reputation: 6813
As suggested in Flask you can use Blueprint for doing larger application. I like the approach of creating modular application that are not too couple with each other. So you do your Blueprints that for the most part have App capabilities suchs as routing or before_request.
simple_page = Blueprint('simple',__name__,template_folder='templates')
@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
try:
return render_template('pages/%s.html' % page)
except TemplateNotFound:
abort(404)
Then you register it:
app = Flask(__name__)
app.register_blueprint(simple_page)
Some catch about blueprint:
You should always remember to append a '.' when trying to get a resource:
url_for('.index') #For flask app
url_for('.index') #For blueprints
Blueprint are fairly new solution in Flask at the time of writing this answer are the best solution for modular application in multiple folder and share the same Flask object amos all the app.
Upvotes: 0
Reputation: 6307
You don't have to define everything in one file. This would be really bad in a bigger app :) Read official short doc and see suggestions there (which include Blueprints mentioned in previous answer and which is really nice way to organize big app).
Also there is a nice sample app on how to organize and create bigger app. Could be helpful too ;)
And other methods, classes and everything which is not Flask specific is just, simple Python, so also no need to have them in one file.
Upvotes: 4
Reputation: 1348
You can move declarations into other files as long as that file has a reference to the Flask
object you created, however if your application is getting big enough to warrant that you should perhaps begin to look into Flask Blueprints
Upvotes: 1