background preloader

The Flask Mega-Tutorial, Part I: Hello, World!

The Flask Mega-Tutorial, Part I: Hello, World!
This is the first article in a series where I will be documenting my experience writing web applications in Python using the Flask microframework. NOTE: This article was revised in September 2014 to be in sync with current versions of Python and Flask. Here is an index of all the articles in the series that have been published to date: My background I'm a software engineer with double digit years of experience developing complex applications in several languages. In addition to Python, I've written web apps in PHP, Ruby, Smalltalk and believe it or not, also in C++. UPDATE: I have written a book titled "Flask Web Development", published in 2014 by O'Reilly Media. The application The application I'm going to develop as part of this tutorial is a decently featured microblogging server that I decided to call microblog. These are some of the topics I will cover as we make progress with this project: So as you see, I'm going pretty much for the whole thing. Requirements Installing Flask #! . Miguel Related:  trenchmortar1

Python Flask and jQuery Ajax POST I have already covered an introductory article on getting started with python web application development using Python Flask and MySQL. In this tutorial, we'll focus on the client side of the web application. We'll see how to use jQuery AJAX along with Python Flask. Prior to getting started with this tutorial, make sure you have Python installed. You can check if it's installed from the command prompt: python --version If python is installed, it'll show the version of python installed. pip install Flask Now create an app directory called FlaskApp, navigate to it and create app.py with the following code: from flask import Flask app = Flask(__name__) @app.route("/")def hello(): return "Welcome to Python Flask!" The python code shown above is quite self explanatory. First we imported the Flask module and then created a flask app instance. Save the app.py and from terminal run: python app.py Your app must be now running. Let's create a Sign Up page called signUp,html. <!

Welcome to Flask MongoAlchemy’s documentation! — Flask MongoAlchemy 0.5.4 documentation Flask-MongoAlchemy adds support for MongoDB on Flask using MongoAlchemy. Source code and issue tracking are available at Github. If you want to get started, check out the example source code. Installation You can easily install using pip or easy_install: $ [sudo] pip install Flask-MongoAlchemy $ [sudo] easy_install Flask-MongoAlchemy If you prefer, you may use the latest source version by cloning the following git repository: $ git clone $ cd flask-mongoalchemy $ [sudo] python setup.py develop Make sure you have MongoDB installed to use it. Usage It is very easy and fun to use Flask-MongoAlchemy to proxy between Python and MongoDB. All you have to do is create a MongoAlchemy object and use it to declare documents. As you can see, extending the Document is all you need to create a document. Now you can create authors and books: And save them: >>> mark_pilgrim.save()>>> dive.save() If you make any changes on a document, you may call save() again:

How to automatically create a form from a model in Flask | Isak Utegard I’ve been playing around a bit with a Python microframework called Flask lately. I like it very much and it’s really simple to get started. You should absolutely check it out if you like Python. I’ve been able to combine Flask with MySQL through Flask-SQLAlchemy and by using Flask-WTF I’ve managed to successfully post data to my database. I’ve tried various approaches where one was this approach where you automatically creates a form from your model like this: from flask import render_template from flask.ext.wtf import Form from wtforms.ext.sqlalchemy.orm import model_form MyForm = model_form(MyModel, base_class=Form) form = MyForm() return render_template('form.html', form=form) I thought that was pretty cool but every column from my database was rendered as an input-field from the HTML template which wasn’t that cool in the end. When this was done there was no problem to iterate over the fields in my HTML template by using this code:

Preface — Explore Flask 1.0 documentation Python 2 versus Python 3¶ As I write this, the Python community is in the midst of a transition from Python 2 to Python 3. The official stance of the Python Software Foundation is as follows: Python 2.x is the status quo, Python 3.x is the present and future of the language. [1] As of version 0.10, Flask runs with Python 3.3. I’m not using it myself currently, and I don’t ever recommend to people things that I don’t believe in myself, so I’m very cautious about recommending Python 3. One reason for holding off on Python 3 is that many common dependencies haven’t been ported yet. Since this book is meant to provide practical advice, I think it makes sense to write with the assumption of Python 2. Flask version 0.10¶ At the time of writing this, 0.10 is the latest version of Flask (0.10.1 to be exact).

Facebook Authentication for Flask Apps Ok, it looks like you want to add Facebook login to your Flask app. If you followed my first tutorial on Flask apps, you might have something like this in your app.py: import os from flask import Flask, render_template, send_from_directory #---------------------------------------- # initialization #---------------------------------------- app = Flask(__name__) app.config.update( DEBUG = True, ) #---------------------------------------- # controllers #---------------------------------------- @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'ico/favicon.ico') @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.route("/") def index(): return render_template('index.html') #---------------------------------------- # launch #---------------------------------------- if __name__ == "__main__": port = int(os.environ.get("PORT", 5000)) app.run(host='0.0.0.0', port=port) register your app on Facebook

Welcome | Flask (A Python Microframework) Welcome to Flask’s documentation. Get started with Installation and then get an overview with the Quickstart. There is also a more detailed Tutorial that shows how to create a small but complete application with Flask. Common patterns are described in the Patterns for Flask section. The rest of the docs describe each component of Flask in detail, with a full reference in the API section. Flask depends on the Werkzeug WSGI toolkit, the Jinja template engine, and the Click CLI toolkit. User’s Guide¶ Flask provides configuration and conventions, with sensible defaults, to get started. API Reference¶ If you are looking for information on a specific function, class or method, this part of the documentation is for you. Additional Notes¶

Lightweight Python Apps with Flask, Bootstrap, and Heroku Welcome! This is a quick start guide for easily and quickly deploying python apps. The goal of this guide is to provide you with a simple, step-by-step flow that you can follow when building small, simple apps from the ground up (which can later turn into large, complex apps). Each technology, including Heroku, Flask, and Twitter Bootstrap, has been carefully chosen to make the process as simple, quick, and painless as possible. Please note that this guide does not explain every step in detail, and assumes you have at some point interacted with either a few of these technologies or related ones. Now for a little introduction about the design decisions made in this quick start guide: Flask is a popular lightweight python framework (or micro-framework), allowing you to develop simple apps extremely quickly. Heroku is above and beyond the best choice for a lone developer or small team, especially considering it is: Alright, now that we've gotten all of that out of the way, let's get started!

Crossplatform Framework for NUI Python : Bien configurer son environnement de développement C'est quand même plus sympa de développer quand notre environnement de développement est bien configuré ! Ce didacticiel est une introduction à quelques bonnes pratiques qui vous permettront d'avoir un environnement de développement Python aux petits oignons : Travailler avec des environnements virtuels et les associer aux projets,Personnaliser le script de démarrage du shell Python,Valider son code Python,Standardiser l'exécution des tests. Pré-requis Je considère dans ce tutorial que vous avez déjà installé Python > 2.7.9 et > 3.4. Ces versions de Python fournissent par défaut pip qui est un outil permettant de télécharger et d'installer des modules Python depuis le Python Package Index (PyPI). 1. Les environnements virtuels Python permettent d'avoir des installations de Python isolées du système et séparées les unes des autres. Installation de Virtualenv pip install virtualenv Exemple d'utilisation mkdir -p ~/virtualenvs virtualenv ~/virtualenvs/project1 (project1) pip install --upgrade pip

Créez vos applications web avec Flask Version en ligne Table des matières Créez vos applications web avec Flask Vous avez appris le Python et aimeriez l’utiliser pour créer votre site web ? C’est possible ! Ce tutoriel est accessible aux débutants, mais demande tout de même quelques pré-requis : connaître le langage Python (un tutoriel est disponible ici)connaître les langages HTML et CSS (tutoriel ici). Voilà un petit exemple de ce que nous réaliserons durant ce cours : Le Web Avant d’attaquer l’apprentissage de Flask, attardons-nous un peu sur de la théorie : le fonctionnement des sites Web. Un serveur, des clients Un serveur, des clients Pour commencer, disons qu'un site web n'est qu'un simple programme exécuté sur un ordinateur. Quoi de plus simple qu’un beau schéma pour introduire le concept ? Vous noterez que j’ai représenté les clients de la même manière que le serveur : par un ordinateur. Qu’est-ce qui différencie un serveur d’un client dans ton schéma alors ? Le client et le serveur communiquent ensemble. Le protocole HTTP

Https with Http in Flask Python

Related: