Computer Science Grade 10 20 min

Introduction to Flask: Setting Up a Python Web Server

Learn how to set up a Flask web server and create basic routes.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Install the Flask library using pip. Write a minimal Python script to create a Flask application. Define a 'route' to handle web requests for a specific URL. Start the Flask development server and view their web application in a browser. Explain the roles of a web server, a web framework, and a client in the request-response cycle. Create multiple routes to serve different content on different URLs. Ever wondered how typing a URL like `google.com` into your browser magically brings up a website? 🤔 Let's pull back the curtain and build our own simple web server to see how it works! In this tutorial, you'll learn how to use Flask, a popular Python web framework, to turn your computer into a web server. We'll create a basic web app...
2

Key Concepts & Vocabulary

TermDefinitionExample Web ServerA computer program that stores website files (like HTML, CSS, and Python code) and delivers them to a user's web browser upon request.When you run your Flask app, your computer acts as a development web server, listening for requests at an address like http://127.0.0.1:5000. Web FrameworkA collection of pre-written code and tools that helps developers build web applications more quickly and easily, without having to write everything from scratch.Flask is a 'micro-framework' for Python. It provides the basic tools for handling web requests and routing, but lets the developer choose other tools. FlaskA lightweight and flexible Python web framework designed to make getting started with web development quick and easy.We use Flask to write the Pyt...
3

Core Syntax & Patterns

Flask App Initialization from flask import Flask app = Flask(__name__) This is the first step in any Flask application. It imports the main Flask class and creates an instance of it. The `__name__` argument helps Flask know where to look for resources. Route Decorator @app.route('/url-path') This is a Python decorator placed directly above a function. It tells Flask that this function should be executed whenever a user visits the specified URL path (e.g., '/', '/about', '/contact'). Running the Development Server if __name__ == '__main__': app.run(debug=True) This block of code checks if the script is being run directly. If it is, `app.run()` starts the built-in development server. Setting `debug=True` provides he...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A student's `app.py` script contains only these three lines: from flask import Flask app = Flask(__name__) app.run(debug=True) After running `python app.py`, what will the student see in their browser when they visit `http://127.0.0.1:5000/`, and why?
A.blank white page, because no content has been defined.
B.'Hello, World!' message, as this is the default Flask response.
C.'Not Found' error, because no route has been defined for the URL `/`.
D.An 'Internal Server Error', because the code is incomplete.
Challenging
Based on the tutorial's definition of a 'web framework', why is using Flask more efficient for building a web application than writing one from scratch using only Python's built-in `socket` library?
A.Flask applications run faster and use less memory than applications built with `socket`.
B.Flask provides pre-written, high-level abstractions like routing decorators, which saves the developer from writing complex, low-level request parsing and handling code.
C.Flask automatically creates a database for the application, which is not possible with the `socket` library.
D.Flask is the only official way to build web applications in Python, as specified by the Python Software Foundation.
Challenging
The line `app = Flask(__name__)` creates an 'instance' of the `Flask` class. Drawing on your Grade 10 knowledge of Object-Oriented Programming (OOP), what does this imply about the `app` variable?
A.`app` is a simple variable that can only store a single string or number.
B.`app` is a function that must be called with parentheses, like `app()`.
C.`app` is a module that contains other Python scripts.
D.`app` is an object that has its own methods (like `.route()` and `.run()`) and attributes.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Full-Stack Web Development: Building a Simple Web Application

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.