Python Computer Science
Beginner
80 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC

Data Types in Backend

In this lesson, you'll learn about common data types in Python and how to apply them in Flask apps. Follow steps to set up a project, handle strings, integers, lists, and dictionaries, and test data processing in a backend context.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson, you'll explore common data types in Python and how to use them in Flask apps. This builds on handling requests, focusing on strings, integers, lists, and dictionaries in a backend context.

    Data types are fundamental in programming, defining the kind of data you can work with. You'll create a Flask app that processes different data types from user inputs. Here's what you'll do:

    1. Set up a new project.
    2. Handle string data.
    3. Work with integers.
    4. Use lists and dictionaries.
    5. Test and process data.

    By the end, you'll understand how to manage various data types in web backends.

    2 - Create a Project Folder

    To start, create a folder for this lesson's project to keep things organised.

    1. On your computer, create a new folder named 'FlaskDataTypes'.
    2. Open VS Code and select File > Open Folder, then choose 'FlaskDataTypes'.

    With your folder ready, next you'll set up the environment.

    3 - Create the Environment

    Create a virtual environment for your project.

    1. Open Command Palette: View > Command Palette.
    2. Type 'Python: Create Environment' and select it.
    3. Choose 'Venv'.
    4. Select your Python version.
    5. VS Code creates a '.venv' folder.

    4 - Create a New Terminal and Install Flask

    Open a terminal in VS Code to install Flask if needed.

    1. Go to View > Terminal.
    2. Run: pip install flask.
    3. Wait for installation to finish.

    5 - Basic Flask App with String Data

    Let's create a Flask app that handles string data from a form.

    Strings are sequences of characters, enclosed in single or double quotes, that represent text data. They can include letters, numbers, symbols, and spaces.

    Examples of strings include names like 'Alice', messages such as 'Hello, World!', or any other textual information. In Flask, you often handle strings from user inputs, such as data submitted through web forms, and process them in your backend logic.

    Create 'app.py' in 'FlaskDataTypes'.

    Add this code:

    from flask import Flask, render_template, request
    
    app = Flask(__name__)
    
    @app.route('/')
    def home():
        return 'Welcome to Data Types in Backend!'
    
    @app.route('/string', methods=['GET', 'POST'])
    def handle_string():
        if request.method == 'POST':
            text = request.form['text']
            return f'You entered the string: {text}'
        else:
            return render_template('string_form.html')

    Create a 'templates' folder, and inside it, 'string_form.html' with:

    <!DOCTYPE html>
    <html>
    <head><title>String Input</title></head>
    <body>
    <h1>Enter a String</h1>
    <form method="post" action="/string">
        <input type="text" name="text">
        <button type="submit">Submit</button>
    </form>
    </body>
    </html>
    Note that the form's action is set to '/string', which matches the route defined in the Flask app. This ensures the form data is sent to the correct endpoint.

    Run python -m flask run.
    Visit http://127.0.0.1:5000/string, enter text, submit, and see the response.
    Stop with Ctrl+C.


    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more