Skip to content

Instantly share code, notes, and snippets.

@aidev13
Created November 29, 2023 00:24
Show Gist options
  • Save aidev13/bfa8a970d862a863f205b2c8d6f8ede6 to your computer and use it in GitHub Desktop.
Save aidev13/bfa8a970d862a863f205b2c8d6f8ede6 to your computer and use it in GitHub Desktop.
Setting up a Flask server for Python

Setting up a Flask server for Python - for Windows

Editor used is VsCode and MUST use powershell for command line

Step 1: Create a Virtual Environment - In the root directory/folder (I choose to use the word folder), run the following command

python -m venv venv

The venv folder should appear.

Step 2: Run the following command from the root of your project

.\venv\Scripts\activate

!!! NOTE, If you are getting an error that resembles the following: cannot be loaded because the execution of scripts is disabled on this system , then enter the following below in the powershell to enable scripts.

Set-ExecutionPolicy unrestricted

or refer to the following lionk for more information

https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts

Step 3: Install Flask by running the following in your powershell terminal

pip install flask
pip is the npm of python

You can verify the installation by inspecting the content of the Lib\site-packages directory in the venv folder. I here you should see tge 'flask' file.

Step 4: Create a new folder in the root of your project called app and inside the app folder make a file called

__init__.py. 

(Thats two underscores on both sides of init). In case your wondering, The __init__.py file of a Python package corresponds to the index.js file of a Node.js module.

Step 5: Open the app/__init__.py file, and enetr the following:

from flask import Flask

def create_app(test_config=None):
  # set up app config
  app = Flask(__name__, static_url_path='/')
  app.url_map.strict_slashes = False
  app.config.from_mapping(
    SECRET_KEY='super_secret_key'
  )

  return app

Step 6: Start the Flask Server - In the root directory of your project, run the following command to start the Flask server:

python -m flask run

you will see the following:

* Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Step 7: Set up a route by modifying the code above with the following and refresh your browser.

def create_app(test_config=None):
 # set up app config
 app = Flask(__name__, static_url_path='/')
 app.url_map.strict_slashes = False
 app.config.from_mapping(
   SECRET_KEY='super_secret_key'
 )

 @app.route('/hello')
 def hello():
   return 'hello world'

 return app

Great work! You now have a basic Flask server.

NOTE your browser address should lkook like this: http://127.0.0.1:5000/hello , and you should see 'hello world'

This is the absoualte basics to setting up a flask server for python. If this does not help, you can always refer to google! Googleing is a skill in in-itself!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment