7 ms·
The Flask Mega-Tutorial
- randomchars 14y ago> If you are on Linux, OS X or Cygwin, install flask and extensions by entering the following commands, one after another I think this is a really bad advice. Instead create a file called requirements.txt and add the required livraries, one per line. Then you can install it with flask/bin/pip install - r requirements.txt Edit: More information in the documentation[0] [0]: http://www.pip-installer.org/en/latest/requirements.html http://www.pip-installer.org/en/latest/requirements.html
- nfg 14y agoYou can always regularise things after with: pip freeze > requirements.txt
- blaze33 14y agoOr with: pip freeze -r requirements.txt So you can keep your file structure with comments, nicely separating your dependencies from the dependencies of your dependencies. This way you'll have a much nicer time the day you need to remove one of them :) And beware of git urls being replaced by egg names in the process.
- deleted 14y ago[deleted]
- buro9 14y agoQuestion: Is there a way to create application modules in the same style of Django modules (shippable extensions to an application) in Flask? I've read about Flask blueprints, but that didn't seem to have the same angle... seemed to be more shippable libraries rather than a module of an application.
- mvermaat 14y agohttp://flask.pocoo.org/docs/extensiondev/ http://flask.pocoo.org/docs/extensiondev/
- irahul 14y agoBlueprints are the same angle(better IMO). Develop the blueprint as a standalone module[1](templates, url rules, static files, before handlers...), and the mount it on the main application. [1] Not python modules. I say module in the abstraction sense. For eg, you can create a blueprint comments, define url rules('/', '/new', ':id/edit'...), add authentication to before handler(check before create, delete, edit), and then mount it on the main application. app.register_blueprint(comments, url_prefix='/comments') You can have blueprints which are self contained modules(comments), blueprints which just add jinja extensions, blueprints which bring in static assets etc etc. What does Django apps do which blueprints don't?
- nonpme 14y agoThe biggest problem I have with Python frameworks is the fact that it's hard (at least for me) to configure server and just write aplications. I came from PHP background, where you install few packages, configure apache/nginx (which is quite fast) and things just work. I'm learning python for some time now, but I still can't create working python environment... I know django basics, and use its server for developing, but I don't know how to configure nginx on my VPS so I can have multiple python webapps (my simple apps, django apps, flask etc.). Someone has good resources for that (believe me, I read a lot of tutorials)?
- derda 14y agoI haven't yet deployed a big app with flask yet, but for my "playground" (Intranet App, very low load) I use Gunicorn (http://gunicorn.org/ http://gunicorn.org/ ), which is extremely easy to set-up. For a deployment you would want to put nginx in front of it to serve static data. If you just want do develop and test some things the server it comes with is absolutely enough and helps a lot with the debugging. Its as easy as typing "python myapp.py".
- nonpme 14y agoI'll check out Gunicorn, thanks for the link.
- antihero 14y agoSkip it and go with uWSGI ;) Emperor mode is fantastic. Say you have /srv/mysite/uwsgi.ini and /srv/myothersite/uwsgi.ini (uwsgi config files for two different sites) you can run uwsgi --emperor /srv/*/uwsgi.ini and bam, it'll launch processes for all your sites.
- pjscott 14y agoFor deployment, it's also important to have something like nginx in front of Gunicorn to buffer requests from slow clients. Otherwise, this sort of thing can DoS your server pretty bad: http://ha.ckers.org/slowloris/ http://ha.ckers.org/slowloris/
- Kiro 14y agoI thought Flask was a minimal micro framework but this looks as bloated as Django.
- the1 14y agoand it uses a lot more RAM compared to bottle.
- the_mitsuhiko 14y agoThat's to be expected. Flask builds on top of Werkzeug, itsdangerous and blinker whereas Bottle reimplements part of what Flask and the dependency libraries provides itself. However for the vast majority of applications having strong foundations to build upon is the right thing to do.
- Goranek 14y agoFlask is awesome, great job!
- Goranek 14y agoyou've figured that out based on a simple hello world, right?
- irahul 14y agoThe difference between Flask's and Bottle's ram usage will be negligible. Out of box, flask is a wrapper over werkzeug and ships with Jinja2. The main memory usage will come from other libraries and extensions, and your own application code.
- brass9 14y agoFlask is anything but "minimal micro". It's a thin Sinatra-like facade over Werkzeug.
- irahul 14y ago> I thought Flask was a minimal micro framework but this looks as bloated as Django. What kind of web applications have you written, and in what language/frameworks? My experience doesn't match yours. Flask is minimal in the sense that it doesn't add ORM, auth, assets etc to the core framework. There is nothing bloated about Flask, or even Django.
- reinhardt 14y agoI'm between Flask and Pyramid for a new project, would love a hands-on comparison between the two.
- irahul 14y agoI prefer keeping routes in a separate file. Instead of @app.route('/') def index(): pass I keep a routes array routes = [ ('/', endpoint='index', view_func=index) ... ] I find it a lot pleasant to look up all routes at the same place rather than guessing or searching. Also, I give an explicit name to all endpoints. I do the same with blueprints. Blueprints have their own urls.py, and blueprints don't specify the url prefix. For eg, a comment blueprint will define '/', rather than '/comments'. I leave the url prefix to the main application. app.register_blueprint(comment, url_prefix='/comment') For migrations, I prefer alembic to sql migrations - https://alembic.readthedocs.org/en/latest/tutorial.html https://alembic.readthedocs.org/en/latest/tutorial.html I find Alembic simpler than SQLAlchemy(without compromising on power). Alembic can autogenerate migrations based on your model definitions. The way I work is I add a new model, and then run alembic to autogenerate the migration. Same goes for altering models. For settings, I maintain setting.py, dev_settings.py, prod_settings.py. settings.py contains all the common settings. If environment is not defined, I do a `from dev_settings import *`, else I do a relevant import for the environment. I also prefer having BEFORE_REQUESTS, AFTER_REQUESTS, MIDDLEWARES, LOG_HANDLERS, TEMPLATE_FILTERS, CONTEXT_PROCESSORS, ERROR_HANDLERS... in settings, and in main.py, call the relevant functions. That way, I don't have to hunt down the codebase. For blueprints, I define the same variables in __init__.py. Model forms are another time saver(use wtforms, or preferably flask-wtform) from flask.ext.wtf import Form from wtforms.ext.sqlalchemy.orm import model_form import models PostForm = model_form(models.Post, models.db.session, Form, field_args = { 'name': {'validators': []}, 'title': {'validators': []}, 'content': {'validators': []}, }) models.Post is flask-sqlalchemy model, models.db.session is sqlalcheymy session. You don't need to list all model fields for your model form. I find keeping models and forms in sync a pain. This way, I only keep the validations in sync(field_args isn't required - that's an example of adding validations). I use flask-debugtoolbar for basic profiling and debugging. flask-webassets is awesome for managing assets and dynamic compilation. flask-script for writing manage.py jobs(python manage.py get_deps, python manage.py db_createall etc etc)
- lifeisstillgood 14y agoOn the routes side it also means you don't fact around having to import a module to get your app - if you can instantiate the app (as it were) and then set up routes you can create a factory much more easily and pass it around with little worries - but anchored to a module import as in the docs is less than helpful Would love to see or hear more about your default setups though. Is there an example in OSS?
- arianvanp 14y agoSo I've always wanted to start learnin some python and python web dev. I've only done webdev in node.js so far, and now I have the question : Django or Flask?
- ctoth 14y agoI'd like to quickly plug Flask-Admin here, it's a much nicer experience than writing constant CRUD code for your administrative interface: https://github.com/MrJoes/Flask-Admin/ https://github.com/MrJoes/Flask-Admin/ The author is quite friendly as well.
- QuarkSpark 14y agojust a simple question: Why do we really need Flask, when we have Django?
- randomchars 14y agoWhy do we need Python when we have PHP? Different people like different things. Would you prefer if the was only one library for every purpose?