flask start development server

  1. Import the Flask class from the flask module:

python from flask import Flask

  1. Create an instance of the Flask class:

python app = Flask(__name__)

  • __name__ is a special variable that is used to determine the root path of the application.

  • Define a route and associated function for the root URL ("/"):

python @app.route('/') def hello(): return 'Hello, World!'

  • The @app.route('/') decorator binds the URL '/' to the hello() function.

  • Run the development server:

python if __name__ == '__main__': app.run(debug=True)

  • The if __name__ == '__main__': block ensures that the development server is only started when the script is executed directly, not when it's imported as a module.
  • app.run(debug=True) starts the development server with debugging enabled.

  • Save the script and execute it:

bash python your_script_name.py

  • Replace your_script_name.py with the name of your Python script.

  • Access the development server in a web browser:

  • Open a web browser and enter the specified URL to see the "Hello, World!" message.

  • Observe the development server console for logs and requests. The debug=True parameter enables debugging information.

  • The console will display information about incoming requests and potential issues with the application.

  • To stop the development server, press Ctrl+C in the terminal where it is running.

  • This terminates the server and stops the execution of the Flask application.