_NB: _If you can use linux please do! I had to use Windows at my company because the IT will not give us linux boxes. Therefore, after a lot of hair pulling I had to figure out how to deploy a flask API.

For this short article, I will not go into detail about the actual creation of the flask API but more about how to deploy. Here is a super-condensed step by step to get a flask API running.

Installation:

pip install flask waitress

You will notice we are installing waitress which is a Windows compatible server that we can use to deploy our flask app on multiple threads. This is actually a really nice way to do it! Here is our API code:

API Code:

from flask import Flask
from waitress import serve

app = Flask(__name__)
@app.route('/api/v1/')
def myendpoint():
    return 'We are computering now'
serve(app, host='0.0.0.0', port=8080, threads=1) #WAITRESS!

As you can see from the above we have a super complicated API that we can call from the browser when running:

python server.py

We should be able to navigate to http://myIP:8080/api/v1and get back the expected response.

So this is all good but now what to do? On Windows how do we ensure this will run on boot? The** Task Scheduler **is how I did it. First, make sure you have admin access then open up the task manager.

When creating a task there are a few very important settings:

  1. Make sure you select the highest privileges box

Image for post

2. Trigger the task to run on startup

Image for post

3. Make sure that you use **pythonw **to run in the background (or else you will have a command prompt sitting around forever)

4. Use the Start Directory where you python file exists.

Image for post

BOOM! With that, you should be able to hit run and test your endpoints. With any luck, the next time you reboot the job should trigger and will be running!

I truly hope this helps some poor soul in the future. Godspeed.

#api #web-development #flask #python #programming

Deploying Flask on Windows
2.95 GEEK