Flask

[Pages:66]Flask

#flask

Table of Contents

About

1

Chapter 1: Getting started with Flask

2

Remarks

2

Versions

2

Examples

2

Installation - Stable

2

Hello World

3

Installation - Latest

3

Installation - Development

3

sphinx

3

py.test

4

tox

4

Chapter 2: Accessing request data

5

Introduction

5

Examples

5

Accessing query string

5

Combined form and query string

5

Accessing form fields

6

Chapter 3: Authorization and authentication

7

Examples

7

Using flask-login extension

7

General idea

7

Create a LoginManager

7

Specify a callback used for loading users

7

A class representing your user

8

Logging the users in

8

I have logged in a user, what now?

9

Logging users out

10

What happens if a user is not logged in and I access the current_user object?

10

What next?

10

Timing out the login session

11

Chapter 4: Blueprints

12

Introduction

12

Examples

12

A basic flask blueprints example

12

Chapter 5: Class-Based Views

14

Examples

14

Basic example

14

Chapter 6: Custom Jinja2 Template Filters

15

Syntax

15

Parameters

15

Examples

15

Format datetime in a Jinja2 template

15

Chapter 7: Deploying Flask application using uWSGI web server with Nginx

16

Examples

16

Using uWSGI to run a flask application

16

Installing nginx and setting it up for uWSGI

17

Enable streaming from flask

18

Set up Flask Application, uWGSI, Nginx - Server Configurations boiler template (default, p

19

Chapter 8: File Uploads

24

Syntax

24

Examples

24

Uploading Files

24

HTML Form

24

Python Requests

24

Save uploads on the server

25

Passing data to WTForms and Flask-WTF

25

PARSE CSV FILE UPLOAD AS LIST OF DICTIONARIES IN FLASK WITHOUT SAVING

26

Chapter 9: Flask on Apache with mod_wsgi

28

Examples

28

WSGI Application wrapper

28

Apache sites-enabled configuration for WSGI

28

Chapter 10: Flask-SQLAlchemy

30

Introduction

30

Examples

30

Installation and Initial Example

30

Relationships: One to Many

30

Chapter 11: Flask-WTF

32

Introduction

32

Examples

32

A simple Form

32

Chapter 12: Message Flashing

33

Introduction

33

Syntax

33

Parameters

33

Remarks

33

Examples

33

Simple Message Flashing

33

Flashing With Categories

34

Chapter 13: Pagination

35

Examples

35

Pagination Route Example with flask-sqlalchemy Paginate

35

Rendering pagination in Jinja

35

Chapter 14: Redirect

37

Syntax

37

Parameters

37

Remarks

37

Examples

37

Simple example

37

Passing along data

37

Chapter 15: Rendering Templates

39

Syntax

39

Examples

39

render_template Usage

39

Chapter 16: Routing

41

Examples

41

Basic Routes

41

Catch-all route

42

Routing and HTTP methods

43

Chapter 17: Sessions

44

Remarks

44

Examples

44

Using the sessions object within a view

44

Chapter 18: Signals

46

Remarks

46

Examples

46

Connecting to signals

46

Custom signals

46

Chapter 19: Static Files

48

Examples

48

Using Static Files

48

Static Files in Production (served by frontend webserver)

49

Chapter 20: Testing

52

Examples

52

Testing our Hello World app

52

Introduction

52

Defining the test

52

Running the test

53

Testing a JSON API implemented in Flask

53

Testing this API with pytest

53

Accessing and manipulating session variables in your tests using Flask-Testing

54

Chapter 21: Working with JSON

57

Examples

57

Return a JSON Response from Flask API

57

Try it with curl

57

Other ways to use jsonify()

57

Receiving JSON from an HTTP Request

57

Try it with curl

58

Credits

59

About

You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: flask

It is an unofficial and free Flask ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Flask.

The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to info@



1

Chapter 1: Getting started with Flask

Remarks

Flask is a Python web application micro-framework built on top of the Werkzeug WSGI library. Flask may be "micro", but it's ready for production use on a variety of needs.

The "micro" in micro-framework means Flask aims to keep the core simple but extensible. Flask won't make many decisions for you, such as what database to use, and the decisions that it does make are easy to change. Everything is up to you, so that Flask can be everything you need and nothing you don't.

The community supports a rich ecosystem of extensions to make your application more powerful and even easier to develop. As your project grows you are free to make the design decisions appropriate for your requirements.

Versions

Version Code Name Release Date

0.12

Punsch

2016-12-21

0.11

Absinthe

2016-05-29

0.10

Limoncello 2013-06-13

Examples

Installation - Stable

Use pip to install Flask in a virtualenv.

pip install flask

Step by step instructions for creating a virtualenv for your project:

mkdir project && cd project python3 -m venv env # or `virtualenv env` for Python 2 source env/bin/activate pip install flask

Never use sudo pip install unless you understand exactly what you're doing. Keep your project in a local virtualenv, do not install to the system Python unless you are using the system package



2

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download