© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
K. WilsonThe Absolute Beginner's Guide to Python Programminghttps://doi.org/10.1007/978-1-4842-8716-3_12

12. Python Web Development

Kevin Wilson1  
(1)
London, UK
 

Python is widely used for developing large-scale web applications that are not possible to build using .NET and PHP.

Python supports features that are executed with different frameworks such as Django, Flask, Pyramid, and CherryPy commonly used in sites such Spotify and Mozilla.

You’ll also need the source files in the directory Chapter11.

Web Servers

Most Python web applications are executed on a web server through an interface called WSGI (web server gateway interface ). Other Python scripts are executed through CGI (common gateway interface ).

Here, we’ve installed the Python interpreter and enabled the WSGI adapter module for the Apache web server.

A diagram exhibits an image of a laptop labeled client web browser connecting to a desktop computer labeled server 192.168.1.3.

Figure 12-1

An example of connecting to a web server on another machine

For this section, you’ll need access to a web server with Python support.

There is a free web server available from Aprelium called Abyss Web Server X1 that you can install on your computer to develop and test websites:

aprelium.com/downloads/

A screenshot of the abyss web server console demonstrates a host table. It includes server and console configurations, S S T / T L S certificates, server statistics, server activity, help and support, and information about the server options.

Figure 12-2

The Aprelium web server running on a local computer

Install the Web Server

  1. 1.

    Open the directory where you have saved the software package.

     
  2. 2.

    Double-click the software package icon.

     
  3. 3.

    Deselect components you do not want to install. Auto start enables Abyss Web Server auto starting when a Windows session starts – deselect this. Start Menu Shortcuts enables adding Abyss Web Server shortcuts in the Start Menu. Documentation installs help files.

     
  4. 4.

    Click Next.

     
  5. 5.

    Choose a directory where you want to install Abyss Web Server files. From now on, <Abyss Web Server directory> will refer to this directory.

     
  6. 6.

    Click Install.

     

Set Up Python Support

  1. 1.

    Open the Abyss Web Server’s Console. Make sure the web server is running, then open your web browser, then enter the following in the address field at the top: 127.0.0.1:9999.

    Type in the admin password you created when you installed the server.

     
  2. 2.

    In the Hosts table, click Configure in the row corresponding to the host to which you want to add Python support.

     
  3. 3.

    Select Scripting Parameters.

     
  4. 4.

    Check Enable Scripts Execution.

     
  5. 5.

    Click Add in the Interpreters table.

     
  6. 6.

    Set Interface to CGI/ISAPI.

     
  7. 7.

    In the Interpreter field, click Browse..., go to the directory where you have installed Python, and click on python.exe.

     
  8. 8.

    Check Use the associated extensions to automatically update the Script Paths .

     
  9. 9.

    Click Add in the Associated Extensions table.

     
  10. 10.

    Enter py in the Extension field and click OK.

     
  11. 11.

    Click OK on interpreters screen.

     
  12. 12.

    Click OK in the Scripting Parameters screen.

     
  13. 13.

    Select Index Files.

     
  14. 14.

    Click Add in the Index Files table.

     
  15. 15.

    Enter index.pj in the File Name field, click OK. Then click OK in the index files screen.

     
  16. 16.

    Click Restart to restart the server.

     

Upload your scripts to your public_html directory on the server; then on your computer, open a web browser and enter the URL to your script:

http://server-name/script-name.py

For example:

http://titan/script.py

If you’re using aprelium personal server on your own computer you can use
http://localhost/script.py
To start writing your Python scripts , you’ll need to tell the web server where to find the Python interpreter. This is usually
#!/python/python
or on a Linux server
#!/usr/bin/python

This is the first line of your script.

Executing a Script

Let’s take a look at an example. Have a look at script.py. Here, we’ve written a script to output a simple HTML page.

A script dot p y window has a 12-line code of outputs for an H T M L page.

This page simply outputs the heading “It works!!.” Upload the script into your public_html directory on your web server, and then navigate to the script URL using your web browser on your computer.

A window of a Python tab has text input that reads it works, with two exclamation marks.

In our lab URL , this would be

http://titan/script.py

Let’s take a look at a practical example. Here, we’re creating a simple contact form . The user is presented with an HTML form that asks for their name, email address, and a message.

A 29-line code window has a boxed 9-line code that creates a contact form, with details to fill out, such as name, email, and message.

When the user clicks the “Submit” button, the HTML form calls a Python script called contactus.py .

A window of a browser with personal details boxed points to a boxed 3-line code of contact form on the contact us dot p y window.

The Python script processes the data passed from the HTML form and stores it in a form object.

We can then get the values passed from the HTML form and store them in this object.

The Python script generates another HTML page using print statements for the response to the user.

A contact us dot p y window has a boxed 14-line code for generating a response page.

You can see the output in the following browser.

A window of a browser in the contact us tab of Titan with output from a code, about a message.

Python Web Frameworks

If you are using Python in web development, you’ll more than likely be using a Python web framework rather than the old CGI we looked at in the previous section.

A Python web framework is a collection of tools, libraries, and technologies that allow you to build a web application.

One example of a Python web framework is Django (pronounced “Jango”).

A window of Django administration has a navigation pane with features, shop, firma, test-pages, and meta as titles, each with details in columns.

Another example is Flask . Let’s take a look at how to create a simple web app using this framework.

The first thing you’ll need to do is install the Flask module if you haven’t already done so. Use the following command in the admin command prompt:
pip install Flask
Use this if you’re on a Linux-based machine:
sudo pip install Flask

Let’s create an app. First thing we need to do is create our main program. To do this, we create a new file called app.py . We’ve included all these files in the Flask directory in the resource files.

A 7-line code about Flask web application, on the app dot p y window.

Here, we’ve imported our Flask module.

Modern web apps use a technique called routing . This means instead of having a URL to a page
localhost/resources.php
we use a route
localhost/resources/

So our first route is the root of our website and is usually the index page. We use @app.route('/') to determine this.

The “/” means the root of the website:
http://localhost:5000/

def index() is the name you give to the route defined earlier. This one is called index, because it’s the index (or home page) of the website.

host='0.0.0.0' means the app is accessible from any machine on the network.

To run and test the app , we need to open it using the development environment. This is a simple web server that allows you to open the app in a web browser for testing.

To do this, open your app directory in the command prompt. In this particular example, the app files are in OneDrive/Documents/Flask.

A code in the command prompt window reads D, colon, backslash, one drive, backslash, documents, backslash, flask, and close angle bracket.

To start the app, type
python app.py

Once you press Enter, the server will start.

A command prompt, python app dot p y window depicts an 8-line code of a Python application in a run.

You can open the app in a web browser. On your own workstation, you can use localhost: 5000 :

A window of a browser with a text that reads this is a Flask web app. An arrow points to a text in the address bar that reads local host, 5000.

To add another page, add another route.

A 3-line code of adding another route for the addition of a page.

Now in the web browser, you can use
localhost:5000/shop

Now that we have our base app, we can develop web pages for the app to call. These web pages are called templates and we store these in a template directory. Here’s a simple HTML page we’ve created and saved in the templates directory.

A 10-line code of an H T M L page with details about Elluminet Press, on the index dot h t m l window.

Let’s call our HTML page from our app. We can use the render_ template() function .

A 7-line code for a template in Flask, on the app 02 dot p y window.

When we view the page in a browser, we’ll see a rendered version of the HTML page.

A browser window on the Elluminet Press page has text input with a header that reads Welcome to Elluminate Press L t d.

You can pass variables to your HTML templates. To do this, embed the variable using {{variable-name}} in your HTML.

Then add the variable as a parameter to the render_template() function:
render_template('index.html', variable-name = "...")

Let’s take a look at an example. Open the file app3.py and index2.html .

An app 03 dot p y window code points to the title and price code in the index 2 dot h t m l window. Also depicted is the window of Elluminet Press welcome page.

Here , we’ve passed the title and price as variables to the HTML template.

You can add images to your templates using HTML and CSS code. You can also embed Python code.

Summary

In this chapter, you learned that
  • Django, Flask, Pyramid, and CherryPy are common Python web development frameworks.

  • Most Python web applications are executed on a web server through an interface called WSGI (web server gateway interface).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.152.166