Python/Flask: Difference between revisions

From Omnia
Jump to navigation Jump to search
Line 44: Line 44:
Run the server:
Run the server:
  flask --app helloworld run
  flask --app helloworld run
NOTE: As a shortcut, if the file is named app.py or wsgi.py, you don’t have to use --app. See Command Line Interface for more details.
* Meaning, just simply rename helloworld.py to app.py (or wsgi.py) and you can simply run run:
flask run


WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

Revision as of 22:10, 13 December 2025

Flask

Installation

## Linux / macOS
mkdir myproject
cd myproject
# python3 -m venv .flask
python3 -m venv .venv
. .venv/bin/activate
## Windows
mkdir myproject
cd myproject
py -3 -m venv .venv
.venv\Scripts\activate
## Next Common Step
# pip install Flask
pip3 install Flask
# End
deactivate

References:

Quick Start

Flask Hello World

helloworld.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>\n"


Run the server:

flask --app helloworld run

NOTE: As a shortcut, if the file is named app.py or wsgi.py, you don’t have to use --app. See Command Line Interface for more details.

  • Meaning, just simply rename helloworld.py to app.py (or wsgi.py) and you can simply run run:
flask run

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

Note:

* Running on http://127.0.0.1:5000

Test the server:

curl http://127.0.0.1:5000
   <p>Hello, World!</p>

References:

Hello World Headers

$ curl -i http://127.0.0.1:5000
HTTP/1.1 200 OK
Server: Werkzeug/3.1.4 Python/3.12.3
Date: Sat, 13 Dec 2025 22:05:42 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 20
Connection: close

<p>Hello, World!</p>

keywords