Now that you have Flask installed, let's create the simplest possible web application. This foundational step will give you a feel for how Flask works before we integrate our machine learning model. Think of this as the "Hello, World!" of web services.
At its core, a Flask application needs just a few lines of Python code. We'll start by creating a Python file, let's name it app.py
. Inside this file, we perform three main actions:
Here's the code for our basic application. Place this in your app.py
file:
# 1. Import Flask
from flask import Flask
# 2. Create an application instance
# Flask uses __name__ to determine the root path of the application
# so it can find resource files relative to the script's location.
app = Flask(__name__)
# 3. Define a route and its corresponding view function
# The @app.route('/') decorator tells Flask that the function hello_world
# should handle requests for the root URL (e.g., http://yourdomain.com/).
@app.route('/')
def hello_world():
# This function is executed when someone accesses the '/' route.
# It simply returns the string "Hello, World!".
return 'Hello, World!'
# Optional: Add this block to run the server directly using 'python app.py'
# This is useful for development.
if __name__ == '__main__':
# debug=True enables debugging mode, which provides helpful error messages
# and automatically reloads the server when code changes.
# Do NOT use debug=True in a production environment!
app.run(debug=True)
Let's break down the if __name__ == '__main__':
block. This is standard Python practice. When you run the script directly (e.g., python app.py
), Python sets the special variable __name__
to the string '__main__'
. This if
condition becomes true, and the code inside it, app.run(debug=True)
, executes. The app.run()
method starts Flask's built-in development web server. Setting debug=True
is convenient during development as it automatically restarts the server when you save changes to your file and provides more detailed error pages if something goes wrong. Remember, never run a production application with debug=True
enabled, as it poses a security risk.
With app.py
saved, open your terminal or command prompt, navigate to the directory where you saved the file, and run it:
python app.py
Alternatively, if you didn't include the if __name__ == '__main__':
block, you can use the flask
command (you might need to tell Flask where your app is first, depending on your setup):
# Tell Flask where your app is (if your file isn't named app.py or wsgi.py)
# On Linux/macOS:
export FLASK_APP=app.py
# On Windows (cmd):
set FLASK_APP=app.py
# On Windows (PowerShell):
$env:FLASK_APP = "app.py"
# Optionally enable development mode (includes debug mode)
# Linux/macOS:
export FLASK_ENV=development
# Windows (cmd):
set FLASK_ENV=development
# Windows (PowerShell):
$env:FLASK_ENV = "development"
# Run the development server
flask run
You should see output similar to this in your terminal:
* Serving Flask app 'app'
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: XXX-XXX-XXX
This output tells you that Flask's development server is running. It's listening for connections on your local machine (127.0.0.1
, also known as localhost
) on port 5000
.
Now, open your web browser and navigate to the address shown: http://127.0.0.1:5000/
.
You should see the text "Hello, World!" displayed on the page. Congratulations! You've just created and run your first web application using Flask.
Let's visualize what just happened when you accessed the URL:
A simple request-response cycle in our basic Flask application.
Your browser sent an HTTP GET request to the server running your Flask application, asking for the resource at the root path (/
). Flask received this request, looked at its routing rules (@app.route('/')
), found a match, and called the associated view function (hello_world()
). This function returned the string 'Hello, World!'
. Flask then packaged this string into an HTTP response and sent it back to your browser, which rendered the text on the page.
This simple example forms the basis for our prediction service. We will expand on this structure by defining a different route (e.g., /predict
), loading our saved machine learning model when the application starts, and modifying the view function to accept input data, use the model to make a prediction, and return that prediction in the response.
© 2025 ApX Machine Learning