I am trying to make a simple flask app using putty but it is not working
here is my hello.py file:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello, World'
command I am running in putty (when in the file directory of hello.py)
pip install flask
python -c "import flask; print(flask.version)"
Output:1.1.2
export FLASK_APP=hello
export FLASK_ENV=development
flask run
when I go to the ip address: http://127.0.0.1:5000/
I get this
enter image description here
also here is the link of the instruction I am following/did:
https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3
if someone could let me know how to make it work would be great!
thank you!
I used my terminal on my window machine instead of putty which is a remote machine
Check your wlan0 inet adress by typing ifconfig in bash and use it, also try to add the following code in your flask app:
if __name__ == '__main__':
app.run(debug=True, port=5000, host='wlan0 IP Add')
Run the application by typing >>sudo python3 hello.py
Did this work?
Related
I want to change the host and port that my app runs on. I set host and port in app.run, but the flask run command still runs on the default 127.0.0.1:8000. How can I change the host and port that the flask command uses?
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3000)
set FLASK_APP=onlinegame
set FLASK_DEBUG=true
python -m flask run
The flask command is separate from the flask.run method. It doesn't see the app or its configuration. To change the host and port, pass them as options to the command.
flask run -h localhost -p 3000
Pass --help for the full list of options.
Setting the SERVER_NAME config will not affect the command either, as the command can't see the app's config.
Never expose the dev server to the outside (such as binding to 0.0.0.0). Use a production WSGI server such as uWSGI or Gunicorn.
gunicorn -w 2 -b 0.0.0.0:3000 myapp:app
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host="localhost", port=8000, debug=True)
Configure host and port like this in the script and run it with
python app.py
You can also use the environment variable FLASK_RUN_PORT, for instance:
export FLASK_RUN_PORT=8000
flask run
* Running on http://127.0.0.1:8000/
Source: The Flask docs.
When you run the application server using the flask run command, the __name__ of the module is not "__main__". So the if block in your code is not executed -- hence the server is not getting bound to 0.0.0.0, as you expect.
For using this command, you can bind a custom host using the --host flag.
flask run --host=0.0.0.0
Source
You can use this 2 environmental variables:
set FLASK_RUN_HOST=0.0.0.0
set FLASK_RUN_PORT=3000
You also can use it:
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5002)
and then in the terminal run this
set FLASK_ENV=development
python app.py
I want to know the correct way to start a flask application. The docs show two different commands:
$ flask -a sample run
and
$ python3.4 sample.py
produce the same result and run the application correctly.
What is the difference between the two and which should be used to run a Flask application?
The flask command is a CLI for interacting with Flask apps. The docs describe how to use CLI commands and add custom commands. The flask run command is the preferred way to start the development server.
Never use this command to deploy publicly, use a production WSGI server such as Gunicorn, uWSGI, Waitress, or mod_wsgi.
As of Flask 2.2, use the --app option to point the command at your app. It can point to an import name or file name. It will automatically detect an app instance or an app factory called create_app. Use the --debug option to run in debug mode with the debugger and reloader.
$ flask --app sample --debug run
Prior to Flask 2.2, the FLASK_APP and FLASK_ENV=development environment variables were used instead. FLASK_APP and FLASK_DEBUG=1 can still be used in place of the CLI options above.
$ export FLASK_APP=sample
$ export FLASK_ENV=development
$ flask run
On Windows CMD, use set instead of export.
> set FLASK_APP=sample
For PowerShell, use $env:.
> $env:FLASK_APP = "sample"
The python sample.py command runs a Python file and sets __name__ == "__main__". If the main block calls app.run(), it will run the development server. If you use an app factory, you could also instantiate an app instance at this point.
if __name__ == "__main__":
app = create_app()
app.run(debug=True)
Both these commands ultimately start the Werkzeug development server, which as the name implies starts a simple HTTP server that should only be used during development. You should prefer using the flask run command over the app.run().
Latest documentation has the following example assuming you want to run hello.py(using .py file extension is optional):
Unix, Linux, macOS, etc.:
$ export FLASK_APP=hello
$ flask run
Windows:
> set FLASK_APP=hello
> flask run
you just need to run this command
python app.py
(app.py is your desire flask file)
but make sure your .py file has the following flask settings(related to port and host)
from flask import Flask, request
from flask_restful import Resource, Api
import sys
import os
app = Flask(__name__)
api = Api(app)
port = 5100
if sys.argv.__len__() > 1:
port = sys.argv[1]
print("Api running on port : {} ".format(port))
class topic_tags(Resource):
def get(self):
return {'hello': 'world world'}
api.add_resource(topic_tags, '/')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=port)
The very simples automatic way without exporting anything is using python app.py see the example here
from flask import (
Flask,
jsonify
)
# Function that create the app
def create_app(test_config=None ):
# create and configure the app
app = Flask(__name__)
# Simple route
#app.route('/')
def hello_world():
return jsonify({
"status": "success",
"message": "Hello World!"
})
return app # do not forget to return the app
APP = create_app()
if __name__ == '__main__':
# APP.run(host='0.0.0.0', port=5000, debug=True)
APP.run(debug=True)
For Linux/Unix/MacOS :-
export FLASK_APP = sample.py
flask run
For Windows :-
python sample.py
OR
set FLASK_APP = sample.py
flask run
You can also run a flask application this way while being explicit about activating the DEBUG mode.
FLASK_APP=app.py FLASK_DEBUG=true flask run
In the flask doco the following description is shown of deploying a flask app under twistd.
twistd web --wsgi myproject.app
I have a foo.py which looks like this
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
So I expected to be able to run that under twistd like this
twistd web --wsgi foo.app
but twistd doesn't like that (just spits out the help text).
What am I doing wrong ?
BTW in case it matters I'm running this in a virtualenv (in which I have installed both flask and twisted) and the current directory when I issue the twistd command contains foo.py .
EDIT: The version of twistd I am using is 18.7.0
I had failed to notice (until prompted to by Peter Gibson's comment ) that after the help text appears the message "No such WSGI application: 'foo.app'" appears.
You need to add the current directory to the PYTHONPATH environment variable. Try
PYTHONPATH=. twistd web --wsgi foo.app
Or on Windows (untested)
set PYTHONPATH=.
twistd web --wsgi foo.app
I keep getting this error flask.cli.NoAppException: The file/path provided (new_app.py) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py it goes away after I restart the Flask server.
I am running flask run in the correct directory where my app is. This just started happening after working for 2 weeks. I've read that it could be due to an import error, but I am not finding any modules that are not installed on my virutalenv.
from flask import Flask
app = Flask(__name__)
app.debug=True
Most likely you haven't set the FLASK_APP environment variable.
To run the application you can either use the flask command or
python’s -m switch with Flask. Before you can do that you need to tell
your terminal the application to work with by exporting the FLASK_APP
environment variable:
$ export FLASK_APP=hello.py
$ flask run * Running on http://127.0.0.1:5000/
If you are on Windows you need to use set
instead of export.
Alternatively you can use python -m flask:
$ export FLASK_APP=hello.py
$ python -m flask run * Running on http://127.0.0.1:5000/
EDIT
If you have FLASK_APP set then try adding this to new_app.py
app.run(debug=True, port=8800)
Or if you're on Windows:
if __name__ == '__main__':
app.run(debug=True, port=8800)
And then just execute the app with python new_app.py.
SetUp
VirtualBox | Ubuntu Server 12.04.2
(flaskve)vks#UbSrVb:~/flaskve$ python --version
Python 2.7.3
ifconfig
192.168.1.100 (the bridge interface on which i interact with VirtualBox)
code I am trying to run.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='192.168.1.100', port=8080, debug=True)
When I do
(flaskve)vks#UbSrVb:~/flaskve$ python start.py
(flaskve)vks#UbSrVb:~/flaskve$
It does not run or do anything, it just returns back to command prompt. Although I am running in debug=True mode.
I then made a new VirtualEnv and install bottle in that. When I tried to run helloworld it shows the same behaviour.
However I then started the python shell on the same virtualenv, imported bottle modules and ran
>>> from bottle import route, run
>>> run(host='192.168.1.100', port=8081, debug=True)
Bottle v0.11.6 server starting up (using WSGIRefServer())...
Listening on http://192.168.1.100:8081/
Hit Ctrl-C to quit.
What could be problem here ?
Even debug does not show anything.
Following link is the output of python -v start.py
http://paste.ubuntu.com/5713138/
The first example uses Flask, not bottle. Maybe you are confusing your code snippets here? :)