Building Web Apps with Python and Flask
eBook - ePub

Building Web Apps with Python and Flask

Learn to Develop and Deploy Responsive RESTful Web Applications Using Flask Framework

Malhar Lathkar

  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Building Web Apps with Python and Flask

Learn to Develop and Deploy Responsive RESTful Web Applications Using Flask Framework

Malhar Lathkar

Book details
Book preview
Table of contents
Citations

About This Book

A practical guide for the rapid web application development with Flask

Key Features

  • Expert-led coverage of core capabilities of Flask, key extensions and its implementation.
  • Explore the Werkzeug toolkit and Jinja Template engine and see how Flask interacts with JavaScript and CSS.
  • Detailed modules on building and deploying RESTful applications using Flask.

Description
This book teaches the reader the complete workflow of developing web applications using Python and its most outperforming microframework, Flask.The book begins with getting you up to speed in developing a strong understanding of the web application development process and how Python is used in developing the applications. You will learn how to write your own first Flask-based web application in Python. You will learn about web gateway interfaces, including CGI and WSGI along with various tools like the Jinja 2 engine, Werkzeug toolkit, and Click toolkit. You will learn and practice the core features of Flask such as URL routing, rendering, handling static assets of a web application, how to handle cookies and sessions, and other HTTP objects. Once you have developed a strong knowledge of Flask, you will now dive deeper into advanced topics that includes Flask extensions for working with relational and NOSQL databases, Flask_WTF, and Flask-Bootstrap. You will explore design patterns, various blueprints on how to build modular and scalable applications, and finally how to deploy the RESTful APIs successfully on your own.

What you will learn

  • Get to know everything about the core capabilities of Flask.
  • Understand the basic building blocks of Flask.
  • Get familiar with advanced features of Flask, including blueprints, Flask extensions, and database connectivity.
  • Get ready to design your own Flask-based web applications and RESTful APIs.
  • Learn to build modular and scalable applications and how to deploy them successfully.

Who this book is for
This book is ideal for Python enthusiasts, open source contributors, and web app developers who intend to add Python web technologies in their skillsets and startup companies. The understanding of the core Python language with intermediate level expertise is required and experience of working with SQL, HTML, CSS, and JavaScript is an added advantage.

Table of Contents
1. Python for CGI
2. WSGI
3. Flask Fundamentals
4. URL Routing
5. Rendering Templates
6. Static Files
7. HTTP Objects
8. Using Databases
9. More Flask Extensions
10. Blueprints and Contexts
11. Web API with Flask
12. Deploying Flask Applications
13. Appendix

About the Author
Malhar Lathkar is an independent software professional, corporate trainer, freelance technical writer, and Subject Matter Expert with an experience of more than three decades. He has trained hundreds of students/professionals in Python, Data Science, Java and Android, PHP and web development, etc.He also has the experience of delivering talks and conducting workshops on various IT topics.He writes regularly in a local newspaper on sports and technology-related current topics. LinkedIn Profile: https://www.linkedin.com/in/malharlathkar

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Building Web Apps with Python and Flask an online PDF/ePUB?
Yes, you can access Building Web Apps with Python and Flask by Malhar Lathkar in PDF and/or ePUB format, as well as other popular books in Computer Science & Web Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2021
ISBN
9789389898835

CHAPTER 1

Python for CGI

Introduction

Common Gateway Interface (CGI) technology is widely used by many web application servers for rendering dynamic content to a client computer's web browser. It is a standard protocol that sends the output of console applications in the form of dynamically generated web pages. Such programs are known as CGI scripts. A Python script can act as a CGI script when web server software such as Apache is configured accordingly. This chapter describes how a Python script can be used as CGI script and how it is executed on a web server.

Structure

Following topics will be discussed in this chapter:
  • Advent of WWW
  • What is CGI?
  • Configuration of Apache server
  • The basics of HTTP protocol
  • Request methods
  • The cgi module
  • Cookies
  • Alternatives to CGI

Objectives

After studying this chapter, you should be able to:
  • Understand the basics of CGI
  • Use Python as CGI script

Advent of WWW

The world has come a long way since the advent of World Wide Web (WWW) in the early 1990s. What started as a conglomerate of computers interconnected through HTTP protocol and having sharable information in the form of web pages, it now has thousands of web servers making huge amounts of dynamically generated content available to millions of users who use interactive web applications with computers and mobile devices.
In the beginning, WWW was just a collection of static web pages that any user could browse through, with the help of web browser software. It sends a request to a certain server (having a unique URL) for a particular resource (generally a web page) hosted on it. Web pages are stored in a certain folder of server's file system designated as document root, named differently in different web servers. For example, in Apache server installation, its name is htdocs. On Windows IIS server, its name is wwwroot. The server maps client's request to the corresponding web page in the document root and renders its HTML content to the client browser.
Following figure shows how an HTTP server interacts with a web browser client:
Figure 1.1: Static web pages
Gradually, efforts were put in to make the content more dynamic and interactive. CGI is an important technology and one of the first ones to be used for building dynamic websites. In this chapter, we shall discuss how CGI works and how Python can be used to develop CGI scripts.

What is CGI?

For a web server to render a dynamically generated web page, it should be able to accept variable input from the client browser (usually a HTML form data) and send it to some executable program to process it and produce output to be sent back to the browser.
Common Gateway Interface (CGI) is a set of standards to be implemented by web server software. The executable programs that send their output in the form of web pages are called CGI scripts. They are actually program codes that may be written in a variety of languages, particularly C/C++, Perl, PHP, Python, etc. The web server software that invokes such executable scripts (to produce HTML output) must implement CGI standard.
The National Center for Supercomputing Applications (NCSA) developed CGI standard specifications in 1993. CGI version 1.1 was published in 1997 and is still in force. The executable scripts are similar to console applications working in a command line interface environment, although their output is rendered as HTML response. In this chapter, we shall see how Python scripts (with .py extension) are used as CGI scripts.
Client web browser requests for a CGI script. A web server that implements CGI standard has a designated folder, typically named cgi-bin, in which these scripts are stored. On receiving a request, the web server looks for and executes the corresponding script. Obviously, runtime environment of the language in which the said script is written must be available for the server.
Following schematic diagram shows the functioning of CGI:
Figure 1.2: Common Gateway Interface (CGI)

Configuration of Apache Server

As mentioned earlier, all popular web server software have adopted the CGI standard. The well-known Apache server is an open source project and is available for Windows as well as Linux. One of the easiest ways to install Apache is using precompiled bundles such as XAMPP (Apache, MariaDB, PHP, and Perl binaries for Windows) or WAMP (a similar bundle of Apache with MySQL and PHP precompiled binaries). Example codes in this chapter are developed and tested using XAMPP server on Windows operating system.
XAMPP is distributed as open source software by Apache Friends project. First step is to download precompiled installer suitable for the architecture of your hardware from https://www.apachefriends.org/download.html and perform wizard-based installation. Examples in this chapter assume that the installation has been done in the C:\XAMPP folder.
If you open this folder, a htdocs folder and a cgi-bin folder (apart from others) will be seen. The htdocs folder is configured as document root of the server, and cgi-bin is the folder to store CGI script.
Before launching the server, it must be configured to accept cgi requests. For that, you need to open Apache's httpd.conf configuration file (found at C:\xampp\apache\conf\httpd.conf) using any text editor (for example, Notepad) and look for a string AddHandler in it.
As the name suggests, this file is used by Apache to enable or disable various settings and modules. The entries in the file with # are disabled (commented). The AddHandler cgi-script directive tells the server which types of files are to be used as cgi scripts. Ensure that the .py extension is added in the list of extensions as follows:
AddHandler cgi-script .cgi .pl .py
It tells the server to treat files with .cgi, .pl, and .py extensions as CGI script. Now you are ready to launch the Apache server. To test the instal...

Table of contents

Citation styles for Building Web Apps with Python and Flask

APA 6 Citation

Lathkar, M. (2021). Building Web Apps with Python and Flask ([edition unavailable]). BPB Publications. Retrieved from https://www.perlego.com/book/2661006/building-web-apps-with-python-and-flask-learn-to-develop-and-deploy-responsive-restful-web-applications-using-flask-framework-pdf (Original work published 2021)

Chicago Citation

Lathkar, Malhar. (2021) 2021. Building Web Apps with Python and Flask. [Edition unavailable]. BPB Publications. https://www.perlego.com/book/2661006/building-web-apps-with-python-and-flask-learn-to-develop-and-deploy-responsive-restful-web-applications-using-flask-framework-pdf.

Harvard Citation

Lathkar, M. (2021) Building Web Apps with Python and Flask. [edition unavailable]. BPB Publications. Available at: https://www.perlego.com/book/2661006/building-web-apps-with-python-and-flask-learn-to-develop-and-deploy-responsive-restful-web-applications-using-flask-framework-pdf (Accessed: 15 October 2022).

MLA 7 Citation

Lathkar, Malhar. Building Web Apps with Python and Flask. [edition unavailable]. BPB Publications, 2021. Web. 15 Oct. 2022.