0

I wrote a Flask application, and also got it working with the commands that you normaly use to start it.

That means:

cd /var/www/BWSWebApp
export FLASK_APP=main.py
flask run --host=0.0.0.0

Explanation (row for row):

cd into the directory the main.py file is in.

Export the main.py file.

Run flask server with an IP-address that can be seen in the whole network.

Now I don't want to manually type that in the console every time I want to start the Flask application. So i thought myself that I could simply write another Python file with the following

import os


os.system('cd /var/www/BWSWebApp')
os.system('export FLASK_APP=main.py')
os.system('flask run --host=0.0.0.0')

But when i run the file it doesn't starts the server, and in the Python console i also don't see any output.

Can anyone help me out with that?

Jannis
  • 43
  • 1
  • 10
  • If you want to run flask server without those command, then you can use app.run(host="0.0.0.0") you need to run the file in which you use this command. – charchit Jul 10 '21 at 17:44
  • Ok, it just worked like you described. Thank you!! – Jannis Jul 10 '21 at 17:57

2 Answers2

0

Just add this at the end of your code:

if __name__ == '__main__':
    app.secret_key = 'super_secret_key' # your app key
    app.debug = True # set to true if you want to enable debug
    app.run(host='0.0.0.0', port=8000) # change port according to your needs

And run via cli just the script:

python your_script_name.py
Pitto
  • 8,229
  • 3
  • 42
  • 51
0

You need to use app.run() command in the main file like this.

if __name__ ==  '__main__':
      app.run(host="0.0.0.0")

So that when you import the file the server doesn't start. You can use other parameters like in run function. Like

app.run(debug=True,port=5000)
charchit
  • 1,492
  • 2
  • 6
  • 17