create a flask server in python

To create a Flask server in Python, follow these steps:

  1. Install Flask using pip command in your terminal or command prompt.
main.py
pip install Flask
18 chars
2 lines
  1. Create a Python file and import Flask.
main.py
from flask import Flask
24 chars
2 lines
  1. Create a Flask instance.
main.py
app = Flask(__name__)
22 chars
2 lines
  1. Define a route using @app.route() decorator.
main.py
@app.route('/')
def hello():
    return 'Hello, World!'
56 chars
4 lines
  1. Run the Flask app using app.run() method.
main.py
if __name__ == '__main__':
    app.run()
41 chars
3 lines
  1. You can run your Flask server using command in your terminal or command prompt.
main.py
python your_file_name.py
25 chars
2 lines

You have successfully created a Flask server in Python.

gistlibby LogSnag