Flask is incredible. since I've made the transition from PHP-based CMS's I've never looked back. if someone is interested in learning more about building larger scale/production apps with flask, I have a series of tutorials at medium to get you started: https://medium.com/@level09 Disclaimer: I'm the creator of enferno ( http://enferno.io ), A flask-based system pre-configured with caching/user auth/basic template/OR…
Curious what made you go this route rather than say a more full featured framework (Django/Rails)? Just based on assumptions, was it the ability to specify each of your preferred components?
Things which aren't magic – Flask and app.route
21–30 of 35 posts
Re: Things which aren't magic – Flask and app.route
#22What's throwing me off -- and I'm sure it's something trivial I'm not seeing -- is how we add the functions to the routes dictionary. When is the decorator's code actually executed? Is that done at import time? My assumption is that its code is executed when its counterpart is called, though clearly that mustn't be the case. Given my understanding from the article, there's a hole: how does serving route "/" know to c…
Plain decorators work how you expect but generators are different. The decorator generator is called at import time, which is when the association between the route and the function is stored.
Re: Things which aren't magic – Flask and app.route
#23Flask's route decorator gives a nice syntax, but it goes against some ideal best practices: * Imports shouldn't have side-effects (like registering functions with flask). * You shouldn't use globals (like the flask app). * Objects (such as the flask app) should be immutable whenever possible. None of these are hard-and-fast rules, and Python code has a tendency to give up purity in favor of syntax, so it's certainly…
Re: Things which aren't magic – Flask and app.route
#24Re: Things which aren't magic – Flask and app.route
#25What's throwing me off -- and I'm sure it's something trivial I'm not seeing -- is how we add the functions to the routes dictionary. When is the decorator's code actually executed? Is that done at import time? My assumption is that its code is executed when its counterpart is called, though clearly that mustn't be the case. Given my understanding from the article, there's a hole: how does serving route "/" know to c…
These are the results of a simple test in the Python console.
>>> def dec(f):
... print "Decorator ran"
... return f
...
>>> @dec
... def decorated(i):
... print "Decorated function ran"
...
Decorator ran
>>> decorated(5)
Decorated function ran
>>> decorated(5)
Decorated function ranRe: Things which aren't magic – Flask and app.route
#26Am I right in understanding that decorators are a form of closures as the decorator function is returning the function declared inside it ?
Of course ultimately a word can mean different things; did you have a more specific question about what a decorator or a closure does?
Re: Things which aren't magic – Flask and app.route
#27Re: Things which aren't magic – Flask and app.route
#28Flask's route decorator gives a nice syntax, but it goes against some ideal best practices: * Imports shouldn't have side-effects (like registering functions with flask). * You shouldn't use globals (like the flask app). * Objects (such as the flask app) should be immutable whenever possible. None of these are hard-and-fast rules, and Python code has a tendency to give up purity in favor of syntax, so it's certainly…
Imports don't have side-effects. Using Flask's route decorators has.
>You shouldn't use globals (like the flask app).
The Flask app is just as much a global as any other class instance in any OOP language. Whether you make it a module-level object or not is your choice.
>Objects (such as the flask app) should be immutable whenever possible.
They hardly are. This is a good rule which nobody follows, and I don't think you'd gain enough advantages through this.
>You need to make sure that you import every file with a request handler, and those imports often end up unused (only imported for their side-effects), which confuses linters and other static analysis tools.
The fact that your app has import side-effects is your fault, this pattern is not at all encouraged by Flask. You probably want to use blueprints.
Re: Things which aren't magic – Flask and app.route
#29The term decorator generator is incredibly misleading, as it implies relation to Python generators . The term decorator factory (or just decorator with parameters ) is preferrable.
Re: Things which aren't magic – Flask and app.route
#30The term decorator generator is incredibly misleading, as it implies relation to Python generators . The term decorator factory (or just decorator with parameters ) is preferrable.