<?xml version="1.0" encoding="UTF-8"?>
<record
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
    xmlns="http://www.loc.gov/MARC21/slim">

  <leader>10060nam a22002177a 4500</leader>
  <datafield tag="999" ind1=" " ind2=" ">
    <subfield code="c">2435</subfield>
    <subfield code="d">2435</subfield>
  </datafield>
  <controlfield tag="003">OSt</controlfield>
  <controlfield tag="005">20200224131928.0</controlfield>
  <controlfield tag="008">200117b           ||||| |||| 00| 0 eng d</controlfield>
  <datafield tag="020" ind1=" " ind2=" ">
    <subfield code="a">978-93-5213-699-5</subfield>
  </datafield>
  <datafield tag="028" ind1=" " ind2=" ">
    <subfield code="b">Allied Informatics, Jaipur</subfield>
    <subfield code="c">7084</subfield>
    <subfield code="d">13/01/2020</subfield>
    <subfield code="q">2019-20</subfield>
  </datafield>
  <datafield tag="040" ind1=" " ind2=" ">
    <subfield code="a">BSDU</subfield>
    <subfield code="b">English</subfield>
    <subfield code="c">BSDU</subfield>
  </datafield>
  <datafield tag="082" ind1=" " ind2=" ">
    <subfield code="a">006.8</subfield>
    <subfield code="b">GRI</subfield>
  </datafield>
  <datafield tag="100" ind1=" " ind2=" ">
    <subfield code="a">Grinberg, Miguel</subfield>
  </datafield>
  <datafield tag="245" ind1=" " ind2=" ">
    <subfield code="a">Flask Web Development: Developing web applications with python</subfield>
  </datafield>
  <datafield tag="250" ind1=" " ind2=" ">
    <subfield code="b">2nd </subfield>
  </datafield>
  <datafield tag="260" ind1=" " ind2=" ">
    <subfield code="a">Mumbai</subfield>
    <subfield code="b">Shroff Publishers &amp; Distributors Pvt. Ltd.</subfield>
    <subfield code="c">2019</subfield>
  </datafield>
  <datafield tag="300" ind1=" " ind2=" ">
    <subfield code="a">291</subfield>
  </datafield>
  <datafield tag="504" ind1=" " ind2=" ">
    <subfield code="a">Take full creative control of your web applications with Flask, the Python-based microframework. With the second edition of this hands-on book, you&#x2019;ll learn Flask from the ground up by developing a complete, real-world application created by author Miguel Grinberg. This refreshed edition accounts for important technology changes that have occurred in the past three years.


Explore the framework&#x2019;s core functionality, and learn how to extend applications with advanced web techniques such as database migrations and an application programming interface. The first part of each chapter provides you with reference and background for the topic in question, while the second part guides you through a hands-on implementation.

If you have Python experience, you&#x2019;re ready to take advantage of the creative freedom Flask provides. Three sections include:

A thorough introduction to Flask: explore web application development basics with Flask and an application structure appropriate for medium and large applications
Building Flasky: learn how to build an open source blogging application step-by-step by reusing templates, paginating item lists, and working with rich text
Going the last mile: dive into unit testing strategies, performance analysis techniques, and deployment options for your Flask application

Contents
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Part I. Introduction to Flask
1. Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Creating the Application Directory 2
Virtual Environments 2
Creating a Virtual Environment with Python 3 3
Creating a Virtual Environment with Python 2 3
Working with a Virtual Environment 4
Installing Python Packages with pip 5
2. Basic Application Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Initialization 7
Routes and View Functions 8
A Complete Application 9
Development Web Server 10
Dynamic Routes 12
Debug Mode 13
Command-Line Options 15
The Request-Response Cycle 17
Application and Request Contexts 17
Request Dispatching 18
The Request Object 19
Request Hooks 20
Responses 21
Flask Extensions 23
v
3. Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Jinja2 Template Engine 26
Rendering Templates 26
Variables 27
Control Structures 28
Bootstrap Integration with Flask-Bootstrap 30
Custom Error Pages 33
Links 36
Static Files 37
Localization of Dates and Times with Flask-Moment 38
4. Web Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Configuration 44
Form Classes 44
HTML Rendering of Forms 47
Form Handling in View Functions 48
Redirects and User Sessions 51
Message Flashing 53
5. Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
SQL Databases 57
NoSQL Databases 58
SQL or NoSQL? 59
Python Database Frameworks 59
Database Management with Flask-SQLAlchemy 61
Model Definition 62
Relationships 64
Database Operations 66
Creating the Tables 66
Inserting Rows 66
Modifying Rows 68
Deleting Rows 68
Querying Rows 68
Database Use in View Functions 71
Integration with the Python Shell 72
Database Migrations with Flask-Migrate 73
Creating a Migration Repository 73
Creating a Migration Script 74
Upgrading the Database 75
Adding More Migrations 76
vi | Table of Contents
6. Email. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Email Support with Flask-Mail 79
Sending Email from the Python Shell 81
Integrating Emails with the Application 81
Sending Asynchronous Email 83
7. Large Application Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Project Structure 85
Configuration Options 86
Application Package 88
Using an Application Factory 88
Implementing Application Functionality in a Blueprint 90
Application Script 93
Requirements File 93
Unit Tests 94
Database Setup 96
Running the Application 97
Part II. Example: A Social Blogging Application
8. User Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Authentication Extensions for Flask 101
Password Security 102
Hashing Passwords with Werkzeug 102
Creating an Authentication Blueprint 105
User Authentication with Flask-Login 107
Preparing the User Model for Logins 107
Protecting Routes 108
Adding a Login Form 109
Signing Users In 111
Signing Users Out 112
Understanding How Flask-Login Works 113
Testing Logins 114
New User Registration 115
Adding a User Registration Form 115
Registering New Users 117
Account Confirmation 118
Generating Confirmation Tokens with itsdangerous 118
Sending Confirmation Emails 120
Account Management 125
Table of Contents | vii
9. User Roles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Database Representation of Roles 127
Role Assignment 131
Role Verification 132
10. User Pro&#x80;les. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Profile Information 137
User Profile Page 138
Profile Editor 141
User-Level Profile Editor 141
Administrator-Level Profile Editor 143
User Avatars 146
11. Blog Posts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Blog Post Submission and Display 151
Blog Posts on Profile Pages 154
Paginating Long Blog Post Lists 155
Creating Fake Blog Post Data 155
Rendering in Pages 157
Adding a Pagination Widget 158
Rich-Text Posts with Markdown and Flask-PageDown 161
Using Flask-PageDown 162
Handling Rich Text on the Server 164
Permanent Links to Blog Posts 165
Blog Post Editor 167
12. Followers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Database Relationships Revisited 171
Many-to-Many Relationships 172
Self-Referential Relationships 174
Advanced Many-to-Many Relationships 174
Followers on the Profile Page 178
Querying Followed Posts Using a Database Join 181
Showing Followed Posts on the Home Page 183
13. User Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Database Representation of Comments 189
Comment Submission and Display 191
Comment Moderation 193
14. Application Programming Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Introduction to REST 199
viii | Table of Contents
Resources Are Everything 200
Request Methods 201
Request and Response Bodies 201
Versioning 202
RESTful Web Services with Flask 203
Creating an API Blueprint 203
Error Handling 204
User Authentication with Flask-HTTPAuth 206
Token-Based Authentication 208
Serializing Resources to and from JSON 210
Implementing Resource Endpoints 213
Pagination of Large Resource Collections 216
Testing Web Services with HTTPie 217
Part III. The Last Mile
15. Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Obtaining Code Coverage Reports 221
The Flask Test Client 224
Testing Web Applications 225
Testing Web Services 228
End-to-End Testing with Selenium 230
Is It Worth It? 234
16. Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Logging Slow Database Performance 237
Source Code Profiling 239
17. Deployment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Deployment Workflow 241
Logging of Errors During Production 242
Cloud Deployment 243
The Heroku Platform 244
Preparing the Application 244
Testing with Heroku Local 253
Deploying with git push 254
Deploying an Upgrade 255
Docker Containers 256
Installing Docker 256
Building a Container Image 257
Running a Container 261
Table of Contents | ix
Inspecting a Running Container 262
Pushing Your Container Image to an External Registry 263
Using an External Database 264
Container Orchestration with Docker Compose 265
Cleaning Up Old Containers and Images 269
Using Docker in Production 270
Traditional Deployments 270
Server Setup 271
Importing Environment Variables 271
Setting Up Logging 272
18. Additional Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Using an Integrated Development Environment (IDE) 275
Finding Flask Extensions 276
Getting Help 276
Getting Involved with Flask 277
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
x | Table of Contents</subfield>
  </datafield>
  <datafield tag="650" ind1=" " ind2=" ">
    <subfield code="a">Python</subfield>
  </datafield>
  <datafield tag="942" ind1=" " ind2=" ">
    <subfield code="2">ddc</subfield>
    <subfield code="c">BK</subfield>
  </datafield>
  <datafield tag="952" ind1=" " ind2=" ">
    <subfield code="0">0</subfield>
    <subfield code="1">0</subfield>
    <subfield code="2">ddc</subfield>
    <subfield code="4">0</subfield>
    <subfield code="7">0</subfield>
    <subfield code="a">BSDU</subfield>
    <subfield code="b">BSDU</subfield>
    <subfield code="c">GEN</subfield>
    <subfield code="d">2020-01-17</subfield>
    <subfield code="g">750.00</subfield>
    <subfield code="l">1</subfield>
    <subfield code="o">006.8 GRI</subfield>
    <subfield code="p">017989</subfield>
    <subfield code="r">2022-10-15 00:00:00</subfield>
    <subfield code="s">2022-10-01</subfield>
    <subfield code="v">750.00</subfield>
    <subfield code="w">2020-01-17</subfield>
    <subfield code="y">BK</subfield>
  </datafield>
</record>
