Sign In
Sign In

Creating a Web Application Using Python Flask

Creating a Web Application Using Python Flask
Hostman Team
Technical writer
Python
23.10.2024
Reading time: 14 min

In this tutorial, we will write a simple web application in Python with a database for user authentication. We will use Windows 10 and work in PyCharm using pipenv. The website will be built with HTML and CSS. The installation of Flask, PyCharm, and pipenv is described in detail in this article.

Throughout this article, we will use testing and educational examples that are not suitable for production implementation. For instance, to verify passwords in the database, you need to store password hashes and compare hashes, not plaintext passwords. Additionally, when working with databases, you should use an ORM rather than writing "raw" SQL (for more on ORM, see here).

To get started, we will create a project directory called Hostman. Within this directory, we will place the templates and static directories, and within the static directory, we will create a css directory. The project structure will look like this:

Hostman
|— templates
|— static
|    — css

Database for Logins and Passwords

Once you have installed Flask and the other tools, you can proceed to work. We will use the SQLite database management system (DBMS) to store user data. It is well-suited for small projects, and its main advantage is its standalone nature: it does not require a server to operate. Additionally, Python includes a built-in module, sqlite3, for working with SQLite. However, if you decide to work with a server-based DBMS, consider using cloud servers like Hostman.

In the project directory, we create a file named db.py. In this file, we will import the sqlite3 module and create a database and a table for logins and passwords:

import sqlite3
db_lp = sqlite3.connect('login_password.db')
cursor_db = db_lp.cursor()
sql_create = '''CREATE TABLE passwords(
login TEXT PRIMARY KEY,
password TEXT NOT NULL);'''

cursor_db.execute(sql_create)
db_lp.commit()
cursor_db.close()
db_lp.close()

Let’s break down what this code does:

  1. We connect to the database using the connect() method. This method will look for the file login_password.db in the project directory. If it does not find it, it will create one automatically.

  2. We create the cursor_db object for interacting with the database.

  3. sql_create is the SQL query for creating the table that holds the logins and passwords.

  4. We execute sql_create using the execute() method.

  5. We save changes to the database with the commit() method.

  6. Finally, we close the cursor_db and db_lp objects to avoid any database issues.

To create the database, run the command:

python db.py

Authentication

Now let’s work on the authentication for our Flask application.

Main Form

First, we will create a form that will be used for authentication into an abstract service. In the templates directory, we create a file named authorization.html with the following content:

<form method="POST">
  <div class="container">
    <label for="Login"><b>Login</b></label>
    <input type="text" placeholder="" name="Login" required>
    <label for="Password"><b>Password</b></label>
    <input type="password" placeholder="" name="Password" required>
    <button type="submit">Login</button>
    <div class="container">
      <span class="reg"><a href="/registration">Registration</a></span>
    </div>
  </div>
</form>

<link rel="stylesheet" href="{{ url_for('static', filename='css/auth.css') }}">

Here, there are three important points:

  1. <form method="POST"> — we indicate that we will be using POST requests.

  2. <label for="Login"> and <label for="Password"> — we mark the Login and Password fields that we will process later using the get() method from the request module.

  3. <link rel="stylesheet" href="{{ url_for('static', filename='css/auth.css') }}"> — we inform HTML where the CSS file is stored.

In the /static/css directory, we create a file named auth.css:

form {
    font: 14px Stem-Regular, arial, sans-serif; /* Choose font */
    border: 1px solid black; /* Color, size, and type of border */
    -webkit-border-radius: 20px; /* Round corners */
    color: #777991; /* Label color */
    width: 50%; /* Form width */
    margin-right: auto; /* Form positioning */
    margin-left: auto; /* Form positioning */
    text-align: center; /* Center text */
}


input[type=text], input[type=password] {
    text-align: center; /* Center text */
    -webkit-border-radius: 4px; /* Round corners */
    width: auto; /* Width */
    padding: 15px 20px; /* Internal padding size */
    margin: 10px 0; /* External margin size */
    margin-right: auto; /* External right margin size */
    margin-left: auto; /* External left margin size */
    display: block; /* Display type */
    border: 1px solid #050c26; /* Color, size, and type of border */
    box-sizing: border-box; /* Size of object relative to parent */
    font: 14px Stem-Regular, arial, sans-serif; /* Choose font */
    color: #777991; /* Text color in input */
}


button {
    font: 16px Stem-medium, arial, sans-serif; /* Choose button font */
    background-color: #454cee; /* Choose background color */
    -webkit-border-radius: 8px; /* Round edges */
    color: white; /* Choose text color */
    padding: 16px 20px; /* Internal padding size */
    margin: 8px 0; /* External margin size */
    border: none; /* No border */
    cursor: pointer; /* Change cursor on hover */
    width: auto; /* Width */
}


button:hover { 
    opacity: 0.9; /* Change button brightness on hover */
}


.container {
    padding: 20px; /* Internal padding size of the container */
}

As a result, our form will look like this:

Image5

Message for Successful Authentication

If the user enters the correct username-password pair, we will display a corresponding message. In the templates directory, we create a file named successfulauth.html with the following content:

<form method="POST">
  <div class="container">
    <label><b>You have successfully logged in</b></label>   
  </div>
</form>

<link rel="stylesheet" href="{{ url_for('static', filename='css/successfulauth.css') }}">

In the /static/css directory, we create a file named successfulauth.css:

form {
    font: 14px Stem-Regular, arial, sans-serif; /* Choose font */
    border: 1px solid black; /* Color, size, and type of border */
    -webkit-border-radius: 20px; /* Round corners */
    color: #777991; /* Label color */
    width: 40%; /* Form width */
    margin-right: auto; /* Form positioning */
    margin-left: auto; /* Form positioning */
    text-align: center; /* Center text */
}
.container {
    padding: 30px; /* Internal padding size of the container */
}

Image2

Message for Unsuccessful Authentication

In the templates directory, we create a file named auth_bad.html with the following content:

<form method="POST">
  <div class="container">
    <label><b>Incorrect username or password</b></label>   
  </div>
</form>  

<link rel="stylesheet" href="{{ url_for('static', filename='css/auth_bad.css') }}">

In the /static/css directory, we create a file named auth_bad.css:

form {
    font: 14px Stem-Regular, arial, sans-serif; /* Choose font */
    border: 1px solid black; /* Color, size, and type of border */
    -webkit-border-radius: 20px; /* Round corners */
    color: #777991; /* Label color */
    width: 40%; /* Form width */
    margin-right: auto; /* Form positioning */
    margin-left: auto; /* Form positioning */
    text-align: center; /* Center text */
}

.container {
    padding: 30px; /* Internal padding size of the container */
}

Image3

Now, we will create a registration form.

Registration

With the registration form, users will be able to create their own accounts. In the templates directory, create a file named registration.html with the following content:

<form method="POST">
  <div class="container">
    <label for="Login"><b>Username</b></label>
    <input type="text" placeholder="" name="Login" required>


    <label for="Password"><b>Password</b></label>
    <input type="password" placeholder="" name="Password" required>   


    <button type="submit">Register</button>
  </div>
</form>  

<link rel="stylesheet" href="{{ url_for('static', filename='css/regis.css') }}">

In the /static/css directory, create a file named regis.css:

form {
    font: 14px Stem-Regular, arial, sans-serif; /* Choose font */
    border: 1px solid black; /* Color, size, and type of border */
    -webkit-border-radius: 20px; /* Round corners */
    color: #777991; /* Label color */
    width: 50%; /* Form width */
    margin-right: auto; /* Form positioning */
    margin-left: auto; /* Form positioning */
    text-align: center; /* Center text */
}

input[type=text], input[type=password] {
    text-align: center; /* Center text */
    -webkit-border-radius: 4px; /* Round corners */
    width: auto; /* Width */
    padding: 15px 20px; /* Internal padding size */
    margin: 10px 0; /* External padding size */
    margin-right: auto; /* External padding size on the right */
    margin-left: auto; /* External padding size on the left */
    display: block; /* Display type */
    border: 1px solid #050c26; /* Color, size, and type of border */
    box-sizing: border-box; /* Size of the object relative to the parent */
    font: 14px Stem-Regular, arial, sans-serif; /* Choose font */
    color: #777991; /* Text color in input */
}

button {
    font: 16px Stem-medium, arial, sans-serif; /* Choose button font */
    background-color: #454cee; /* Choose background color */
    -webkit-border-radius: 8px; /* Round corners */
    color: white; /* Choose text color */
    padding: 16px 20px; /* Internal padding size */
    margin: 8px 0; /* External padding size */
    border: none; /* No border */
    cursor: pointer; /* Change cursor when hovering over the button */
    width: auto; /* Width */
}

button:hover { 
    opacity: 0.9; /* Change button brightness when hovering */
}

.container {
    padding: 20px; /* Internal padding size of the container */
}

The registration form will look like this:

Image4

Upon successful registration, the user will see a message like this:

Image1

To create this message, in the templates directory, create a file named successfulregis.html with the following content:

<form method="POST">
  <div class="container">
    <label><b>You have successfully registered</b></label>    


    <div class="container">
      <span class="reg"> <a href="/authorization">Return to login</a></span>
    </div>
  </div>
</form>  

<link rel="stylesheet" href="{{ url_for('static', filename='css/successfulregis.css') }}">

In the /static/css directory, create a file named successfulregis.css:

form {
    font: 14px Stem-Regular, arial, sans-serif; /* Choose font */
    border: 1px solid black; /* Color, size, and type of border */
    -webkit-border-radius: 20px; /* Round corners */
    color: #777991; /* Label color */
    width: 25%; /* Form width */
    margin-right: auto; /* Form positioning */
    margin-left: auto; /* Form positioning */
    text-align: center; /* Center text */
}

.container {
    padding: 20px; /* Internal padding size of the container */
}

Authorization Decorator

After creating all the forms and the database, we can start developing the Flask web application. To send an HTML document in response to a client request in Flask, the render_template() method must be used. This function from the Flask module is used to display templates from the templates folder.

The project directory now has the following structure:

Hostman
|— db.py
|— login_password.db
|— templates
|     — authorization.html
|     — auth_bad.html
|     — successfulauth.html
|     — successfulregis.html
|     — registration.html
|— static
|    — css
|         — auth_bad.css
|         — auth.css
|         — successfulauth.css
|         — regis.css
|         — successfulregis.css

In the /Hostman project directory, create a file named main.py, where we will import the necessary modules from Flask and add the code for user authorization. For more details on authentication, we recommend reading here.

from flask import Flask, request, render_template
import sqlite3


@app.route('/authorization', methods=['GET', 'POST'])
def form_authorization():
   if request.method == 'POST':
       Login = request.form.get('Login')
       Password = request.form.get('Password')


       db_lp = sqlite3.connect('login_password.db')
       cursor_db = db_lp.cursor()
       cursor_db.execute(('''SELECT password FROM passwords
                                               WHERE login = '{}';
                                               ''').format(Login))
       pas = cursor_db.fetchall()


       cursor_db.close()
       try:
           if pas[0][0] != Password:
               return render_template('auth_bad.html')
       except:
           return render_template('auth_bad.html')


       db_lp.close()
       return render_template('successfulauth.html')


   return render_template('authorization.html')

Here is the logic of how it works:

  1. The user navigates to /authorization — this is a GET request, and the decorator returns authorization.html.

  2. When the user enters their username and password and clicks the "Login" button, the server receives a POST request, which the decorator will handle. The decorator will retrieve the username and password entered by the user.

  3. Next, we connect to the database and execute an SQL query. Using cursor_db.execute() and cursor_db.fetchall(), we get the row with the password (which may be empty) corresponding to the entered username.

  4. We "extract" the password from the row, and:

    • If the row is empty, it will raise an error (array index out of bounds), which we handle with a try-except block, notifying the user of invalid input. The decorator completes its work.

    • If the password in the database does not match the received password, it simply returns a message about incorrect data and ends the operation.

    • If the password is correct, we display a message about successful authorization and conclude the work of the Flask decorator.

Registration Decorator

Users can access the /registration page from the authorization form. Here is the code for the decorator, which we will add to the end of main.py:

@app.route('/registration', methods=['GET', 'POST'])
def form_registration():
   if request.method == 'POST':
       Login = request.form.get('Login')
       Password = request.form.get('Password')


       db_lp = sqlite3.connect('login_password.db')
       cursor_db = db_lp.cursor()
       sql_insert = '''INSERT INTO passwords VALUES('{}','{}');'''.format(Login, Password)


       cursor_db.execute(sql_insert)
       db_lp.commit()


       cursor_db.close()
       db_lp.close()


       return render_template('successfulregis.html')


   return render_template('registration.html')

Initially, the GET request for /registration is processed, returning registration.html. When the user enters their data and clicks the "Register" button, the server receives a POST request, which extracts the Login and Password.

Next, we connect to the database. The sql_insert variable holds the SQL query for adding a new row with the user's data. We execute sql_insert and save the changes. Finally, we close the cursor_db and db_lp objects, returning a message indicating successful registration.

Full Program Code

from flask import Flask, request, render_template
import sqlite3


app = Flask(__name__)


@app.route('/authorization', methods=['GET', 'POST'])
def form_authorization():
   if request.method == 'POST':
       Login = request.form.get('Login')
       Password = request.form.get('Password')


       db_lp = sqlite3.connect('login_password.db')
       cursor_db = db_lp.cursor()
       cursor_db.execute(('''SELECT password FROM passwords
                                               WHERE login = '{}';
                                               ''').format(Login))
       pas = cursor_db.fetchall()


       cursor_db.close()
       try:
           if pas[0][0] != Password:
               return render_template('auth_bad.html')
       except:
           return render_template('auth_bad.html')


       db_lp.close()
       return render_template('successfulauth.html')


   return render_template('authorization.html')


@app.route('/registration', methods=['GET', 'POST'])
def form_registration():
   if request.method == 'POST':
       Login = request.form.get('Login')
       Password = request.form.get('Password')


       db_lp = sqlite3.connect('login_password.db')
       cursor_db = db_lp.cursor()
       sql_insert = '''INSERT INTO passwords VALUES('{}','{}');'''.format(Login, Password)

This completes the implementation of the user registration decorator and the overall application.

Conclusion

In this tutorial, we covered the essential steps to create a simple web application for user authentication and registration using Python framework Flask. We began by setting up the project structure and configuring SQLite for user data storage. We then developed HTML forms for authorization and registration, styling them with CSS. Through decorators, we implemented the logic to handle user input and interact with the database, ensuring secure processing of credentials. Finally, users receive feedback on successful or failed actions. This foundational knowledge equips you to build more complex Flask applications while enhancing user experience and security.

On our app platform you can deploy Flask and other Python applications. 

Python
23.10.2024
Reading time: 14 min

Similar

Python

The Walrus Operator in Python

The first question newcomers often ask about the walrus operator in Python is: why such a strange name? The answer lies in its appearance. Look at the Python walrus operator: :=. Doesn't it resemble a walrus lounging on a beach, with the symbols representing its "eyes" and "tusks"? That's how it earned the name. How the Walrus Operator Works Introduced in Python 3.8, the walrus operator allows you to assign a value to a variable while returning that value in a single expression. Here's a simple example: print(apples = 7) This would result in an error because print expects an expression, not an assignment. But with the walrus operator: print(apples := 7) The output will be 7. This one-liner assigns the value 7 to apples and returns it simultaneously, making the code compact and clear. Practical Examples Let’s look at a few examples of how to use the walrus operator in Python. Consider a program where users input phrases. The program stops if the user presses Enter. In earlier versions of Python, you'd write it like this: expression = input('Enter something or just press Enter: ') while expression != '': print('Great!') expression = input('Enter something or just press Enter: ') print('Bored? Okay, goodbye.') This works, but we can simplify it using the walrus operator, reducing the code from five lines to three: while (expression := input('Enter something or just press Enter: ')) != '': print('Great!') print('Bored? Okay, goodbye.') Here, the walrus operator allows us to assign the user input to expression directly inside the while loop, eliminating redundancy. Key Features of the Walrus Operator: The walrus operator only assigns values within other expressions, such as loops or conditions. It helps reduce code length while maintaining clarity, making your scripts more efficient and easier to read. Now let's look at another example of the walrus operator within a conditional expression, demonstrating its versatility in Python's modern syntax. Using the Walrus Operator with Conditional Constructs Let’s write a phrase, assign it to a variable, and then find a word in this phrase using a condition: phrase = 'But all sorts of things and weather must be taken in together to make up a year and a sphere...' word = phrase.find('things') if word != -1: print(phrase[word:]) The expression [word:] allows us to get the following output: things and weather must be taken in together to make up a year and a sphere... Now let's shorten the code using the walrus operator. Instead of: word = phrase.find('things') if word != -1: print(phrase[word:]) we can write: if (word := phrase.find('things')) != -1: print(phrase[word:]) In this case, we saved a little in volume but also reduced the number of lines. Note that, despite the reduced time for writing the code, the walrus operator doesn’t always simplify reading it. However, in many cases, it’s just a matter of habit, so with practice, you'll learn to read code with "walruses" easily. Using the Walrus Operator with Numeric Expressions Lastly, let’s look at an example from another area where using the walrus operator helps optimize program performance: numerical operations. We will write a simple program to perform exponentiation: def pow(number, power): print('Calling pow') result = 1 while power: result *= number power -= 1 return result Now, let’s enter the following in the interpreter: >>> [pow(number, 2) for number in range(3) if pow(number, 2) % 2 == 0] We get the following output: Calling pow Calling pow Calling pow Calling pow Calling pow [0, 4, 16] Now, let's rewrite the input in the interpreter using the walrus operator: >>> [p for number in range(3) if (p := pow(number, 2)) % 2 == 0] Output: Calling pow Calling pow Calling pow [0, 4, 16] As we can see, the code hasn’t shrunk significantly, but the number of function calls has nearly been halved, meaning the program will run faster! Conclusion In conclusion, the walrus operator (:=) introduced in Python 3.8 streamlines code by allowing assignment and value retrieval in a single expression. This operator enhances readability and efficiency, particularly in loops and conditional statements. Through practical examples, we’ve seen how it reduces line counts and minimizes redundant function calls, leading to faster execution. With practice, developers can master the walrus operator, making their code cleaner and more concise. On our app platform you can deploy Python applications, such as Celery, Django, FastAPI and Flask. 
23 October 2024 · 4 min to read
Python

Python String Functions

As the name suggests, Python 3 string functions are designed to perform various operations on strings. There are several dozen string functions in the Python programming language. In this article, we will cover the most commonly used ones and several special functions that may be less popular but are still useful. They can be helpful not only for formatting but also for data validation. List of Basic String Functions for Text Formatting First, let’s discuss string formatting functions, and to make the learning process more enjoyable, we will use texts generated by a neural network in our examples. capitalize() — Converts the first character of the string to uppercase, while all other characters will be in lowercase: >>> phrase = 'the shortage of programmers increases the significance of DevOps. After the presentation, developers start offering their services one after another, competing with each other for DevOps.' >>> phrase.capitalize() 'The shortage of programmers increases the significance of devops. after the presentation, developers start offering their services one after another, competing with each other for devops.' casefold() — Returns all elements of the string in lowercase: >>> phrase = 'Cloud providers offer scalable computing resources and services over the internet, enabling businesses to innovate quickly. They support various applications, from storage to machine learning, while ensuring reliability and security.' >>> phrase.casefold() 'cloud providers offer scalable computing resources and services over the internet, enabling businesses to innovate quickly. they support various applications, from storage to machine learning, while ensuring reliability and security.' center() — This method allows you to center-align strings: >>> text = 'Python is great for writing AI' >>> newtext = text.center(40, '*') >>> print(newtext) *****Python is great for writing AI***** A small explanation: The center() function has two arguments: the first (length of the string for centering) is mandatory, while the second (filler) is optional. In the operation above, we used both. Our string consists of 30 characters, so the remaining 10 were filled with asterisks. If the second attribute were omitted, spaces would fill the gaps instead. upper() and lower() — convert all characters to uppercase and lowercase, respectively: >>> text = 'Projects using Internet of Things technology are becoming increasingly popular in Europe.' >>> text.lower() 'projects using internet of things technology are becoming increasingly popular in europe.' >>> text.upper() 'PROJECTS USING INTERNET OF THINGS TECHNOLOGY ARE BECOMING INCREASINGLY POPULAR IN EUROPE.' replace() — is used to replace a part of the string with another element: >>> text.replace('Europe', 'USA') 'Projects using Internet of Things technology are becoming increasingly popular in the USA.' The replace() function also has an optional count attribute that specifies the maximum number of replacements if the element to be replaced occurs multiple times in the text. It is specified in the third position: >>> text = 'hooray hooray hooray' >>> text.replace('hooray', 'hip', 2) 'hip hip hooray' strip() — removes identical characters from the edges of a string: >>> text = 'ole ole ole' >>> text.strip('ole') 'ole' If there are no symmetrical values, it will remove what is found on the left or right. If the specified characters are absent, the output will remain unchanged: >>> text.strip('ol') 'e ole ole' >>> text.strip('le') 'ole ole o' >>> text.strip('ura') 'ole ole ole' title() — creates titles, capitalizing each word: >>> texttitle = 'The 5G revolution: transforming connectivity. How next-gen networks are shaping our digital future' >>> texttitle.title() 'The 5G Revolution: Transforming Connectivity. How Next-Gen Networks Are Shaping Our Digital Future' expandtabs() — changes tabs in the text, which helps with formatting: >>> clublist = 'Milan\tReal\tBayern\tArsenal' >>> print(clublist) Milan Real Bayern Arsenal >>> clublist.expandtabs(1) 'Milan Real Bayern Arsenal' >>> clublist.expandtabs(5) 'Milan Real Bayern Arsenal' String Functions for Value Checking Sometimes, it is necessary to count a certain number of elements in a sequence or check if a specific value appears in the text. The following string functions solve these and other tasks. count() — counts substrings (individual elements) that occur in a string. Let's refer again to our neural network example: >>> text = "Cloud technologies significantly accelerate work with neural networks and AI. These technologies are especially important for employees of large corporations operating in any field — from piloting spacecraft to training programmers." >>> element = "o" >>> number = text.count(element) >>> print("The letter 'o' appears in the text", number, "time(s).") The letter 'o' appears in the text 19 time(s). As a substring, you can specify a sequence of characters (we'll use text from the example above): >>> element = "ob" >>> number = text.count(element) >>> print("The combination 'ob' appears in the text", number, "time(s).") The combination 'in' appears in the text 5 time(s). Additionally, the count() function has two optional numerical attributes that specify the search boundaries for the specified element: >>> element = "o" >>> number = text.count(element, 20, 80) >>> print("The letter 'o' appears in the specified text fragment", number, "time(s).") The letter 'o' appears in the specified text fragment 6 time(s). The letter 'o' appears in the specified text fragment 6 time(s). find() — searches for the specified value in the string and returns the smallest index. Again, we will use the example above: >>> print(text.find(element)) 7 This output means that the first found letter o is located at position 7 in the string (actually at position 8, because counting in Python starts from zero). Note that the interpreter ignored the capital letter O, which is located at position zero. Now let's combine the two functions we've learned in one code: >>> text = "Cloud technologies significantly accelerate work with neural networks and AI. These technologies are especially important for employees of large corporations operating in any field — from piloting spacecraft to training programmers." >>> element = "o" >>> number = text.count(element, 20, 80) >>> print("The letter 'o' appears in the specified text fragment", number, "time(s), and the first time in the whole text at", (text.find(element)), "position.") The letter 'o' appears in the specified text fragment 3 time(s), and the first time in the whole text at 7 position. index() — works similarly to find(), but will raise an error if the specified value is absent: Traceback (most recent call last): File "C:\Python\text.py", line 4, in <module> print(text.index(element)) ValueError: substring not found Here's what the interpreter would return when using the find() function in this case: -1 This negative position indicates that the value was not found. enumerate() — a very useful function that not only iterates through the elements of a list or tuple, returning their values, but also returns the ordinal number of each element: team_scores = [78, 74, 56, 53, 49, 47, 44] for number, score in enumerate(team_scores, 1): print(str(number) + '-th team scored ' + str(score) + ' points.') To output the values with their ordinal numbers, we introduced a few variables: number for ordinal numbers, score for the values of the list, and str indicates a string. And here’s the output: 1-th team scored 78 points. 2-th team scored 74 points. 3-th team scored 56 points. 4-th team scored 53 points. 5-th team scored 49 points. 6-th team scored 47 points. 7-th team scored 44 points. Note that the second attribute of the enumerate() function is the number 1, otherwise Python would start counting from zero. len() — counts the length of an object, i.e., the number of elements that make up a particular sequence: >>> len(team_scores) 7 This way, we counted the number of elements in the list from the example above. Now let's ask the neural network to write a string again and count the number of characters in it: >>> network = 'It is said that artificial intelligence excludes the human factor. But do not forget that the human factor is still present in the media and government structures.' >>> len(network) 162 Special String Functions in Python join() — allows you to convert lists into strings: >>> cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio'] >>> cities_str = ', '.join(cities) >>> print('Cities in one line:', cities_str) Cities in one line: New York, Los Angeles, Chicago, Houston, Phoenix, Philadelphia, San Antonio print() — provides a printed representation of any object in Python: >>> cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio'] >>> print(cities) ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio'] type() — returns the type of the object: >>> type(cities) <class 'list'> We found out that the object from the previous example is a list. This is useful for beginners, as they may initially confuse lists with tuples, which have different functionalities and are handled differently by the interpreter. map() — is a fairly efficient replacement for a for loop, allowing you to iterate over the elements of an iterable object, applying a built-in function to each of them. For example, let's convert a list of string values into integers using the int function: >>> numbers_list = ['4', '7', '11', '12', '17'] >>> list(map(int, numbers_list)) [4, 7, 11, 12, 17] As we can see, we used the list() function, "wrapping" the map() function in it—this was necessary to avoid the following output: >>> numbers_list = ['4', '7', '11', '12', '17'] >>> map(int, numbers_list) <map object at 0x0000000002E272B0> This is not an error; it simply produces the ID of the object, and the program will continue to run. However, the list() method is useful in such cases to get the desired list output. Of course, we haven't covered all string functions in Python. Still, this set will already help you perform a large number of operations with strings and carry out various transformations (programmatic and mathematical). On our app platform you can deploy Python applications, such as Celery, Django, FastAPI and Flask. 
23 October 2024 · 9 min to read
Python

Deploying Python Applications with Gunicorn

In this article, we’ll show how to set up an Ubuntu 20.04 server and install and configure the components required for deploying Python applications. We’ll configure the WSGI server Gunicorn to interact with our application. Gunicorn will serve as an interface that converts client requests via the HTTP protocol into Python function calls executed by the application. Then, we will configure Nginx as a reverse proxy server for Gunicorn, which will forward requests to the Gunicorn server. Additionally, we will cover securing HTTP connections with an SSL certificate or using other features like load balancing, caching, etc. These details can be helpful when working with cloud services like those provided by Hostman. Creating a Python Virtual Environment To begin, we need to update all packages: sudo apt update Ubuntu provides the latest version of the Python interpreter by default. Let’s check the installed version using the following command: python3 --version Example output: Python 3.10.12 We’ll set up a virtual environment to ensure that our project has its own dependencies, separate from other projects. First, install the virtualenv package, which allows you to create virtual environments: sudo apt-get install python3-venv python3-dev Next, create a folder for your project and navigate into it: mkdir myappcd myapp Now, create a virtual environment: python3 -m venv venv And create a folder for your project: mkdir app Your project directory should now contain two items: app and venv. You can verify this using the standard Linux command to list directory contents: ls Expected output: myapp venv You need to activate the virtual environment so that all subsequent components are installed locally for the project: source venv/bin/activate Installing and Configuring Gunicorn Gunicorn (Green Unicorn) is a Python WSGI HTTP server for UNIX. It is compatible with various web frameworks, fast, easy to implement, and uses minimal server resources. To install Gunicorn, run the following command: pip install gunicorn WSGI and Python WSGI (Web Server Gateway Interface) is the standard interface between a Python application running on the server side and the web server itself, such as Nginx. A WSGI server interacts with the application, allowing you to run code when handling requests. Typically, the application is provided as an object named application in a Python module, which is made available to the server. In the standard wsgi.py file, there is usually a callable application. For example, let’s create such a file using the nano text editor: nano wsgi.py Add the following simple code to the file: from aiohttp import web async def index(request): return web.Response(text="Welcome home!") app = web.Application() app.router.add_get('/', index) In the code above, we import aiohttp, a library that provides an asynchronous HTTP client built on top of asyncio. HTTP requests are a classic example of where asynchronous handling is ideal, as they involve waiting for server responses, during which other code can execute efficiently. This library allows sequential requests to be made without waiting for the first response before sending a new one. It’s common to run aiohttp servers behind Nginx. Running the Gunicorn Server You can launch the server using the following command template: gunicorn [OPTIONS] [WSGI_APP] Here, [WSGI_APP] consists of $(MODULE_NAME):$(VARIABLE_NAME) and [OPTIONS] is a set of parameters for configuring Gunicorn. A simple command would look like this: gunicorn wsgi:app To restart Gunicorn, you can use: sudo systemctl restart gunicorn Systemd Integration systemd is a system and service manager that allows for strict control over processes, resources, and permissions. We’ll create a socket that systemd will listen to, automatically starting Gunicorn in response to traffic. Configuring the Gunicorn Service and Socket First, create the service configuration file: sudo nano /etc/systemd/system/gunicorn.service Add the following content to the file: [Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] Type=notify User=someuser Group=someuser RuntimeDirectory=gunicorn WorkingDirectory=/home/someuser/myapp ExecStart=/path/to/venv/bin/gunicorn wsgi:app ExecReload=/bin/kill -s HUP $MAINPID KillMode=mixed TimeoutStopSec=5 PrivateTmp=true [Install] WantedBy=multi-user.target Make sure to replace /path/to/venv/bin/gunicorn with the actual path to the Gunicorn executable within your virtual environment. It will likely look something like this: /home/someuser/myapp/venv/bin/gunicorn. Next, create the socket configuration file: sudo nano /etc/systemd/system/gunicorn.socket Add the following content: [Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock SocketUser=www-data [Install] WantedBy=sockets.target Enable and start the socket with: systemctl enable --now gunicorn.socket Configuring Gunicorn Let's review some useful parameters for Gunicorn in Python 3. You can find all possible parameters in the official documentation. Sockets -b BIND, --bind=BIND — Specifies the server socket. You can use formats like: $(HOST), $(HOST):$(PORT). Example: gunicorn --bind=127.0.0.1:8080 wsgi:app This command will run your application locally on port 8080. Worker Processes -w WORKERS, --workers=WORKERS — Sets the number of worker processes. Typically, this number should be between 2 to 4 per server core. Example: gunicorn --workers=2 wsgi:app Process Type -k WORKERCLASS, --worker-class=WORKERCLASS — Specifies the type of worker process to run. By default, Gunicorn uses the sync worker type, which is a simple synchronous worker that handles one request at a time. Other worker types may require additional dependencies. Asynchronous worker processes are available using Greenlets (via Eventlet or Gevent). Greenlets are a cooperative multitasking implementation for Python. The corresponding parameters are eventlet and gevent. We will use an asynchronous worker type compatible with aiohttp: gunicorn wsgi:app --bind localhost:8080 --worker-class aiohttp.GunicornWebWorker Access Logging You can enable access logging using the --access-logfile flag. Example: gunicorn wsgi:app --access-logfile access.log Error Logging To specify an error log file, use the following command: gunicorn wsgi:app --error-logfile error.log You can also set the verbosity level of the error log output using the --log-level flag. Available log levels in Gunicorn are: debug info warning error critical By default, the info level is set, which omits debug-level information. Installing and Configuring Nginx First, install Nginx with the command: sudo apt install nginx Let’s check if the Nginx service can connect to the socket created earlier: sudo -u www-data curl --unix-socket /run/gunicorn.sock http If successful, Gunicorn will automatically start, and you'll see the HTML code from the server in the terminal. Nginx configuration involves adding config files for virtual hosts. Each proxy configuration should be stored in the /etc/nginx/sites-available directory. To enable each proxy server, create a symbolic link to it in /etc/nginx/sites-enabled. When Nginx starts, it automatically loads all proxy servers in this directory. Create a new configuration file: sudo nano /etc/nginx/sites-available/myconfig.conf Then create a symbolic link with the command: sudo ln -s /etc/nginx/sites-available/myconfig.conf /etc/nginx/sites-enabled Nginx must be restarted after any changes to the configuration file to apply the new settings. First, check the syntax of the configuration file: nginx -t Then reload Nginx: nginx -s reload Conclusion Gunicorn is a robust and versatile WSGI server for deploying Python applications, offering flexibility with various worker types and integration options like Nginx for load balancing and reverse proxying. Its ease of installation and configuration, combined with detailed logging and scaling options, make it an excellent choice for production environments. By utilizing Gunicorn with frameworks like aiohttp and integrating it with Nginx, you can efficiently serve Python applications with improved performance and resource management.
23 October 2024 · 7 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support