I am new to python and trying to create a simple API. below is the code for the same.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)
I have saved this as code as "hello-world.py".
When I am trying to run this file in command prompt using python command
python hello-world.py
Command executed in command prompt
Here I have no error but web page is not getting displayed. Below is the error on web page.
http://localhost:5000/
This site can’t be reached
localhost refused to connect.
Search Google for localhost 5000
ERR_CONNECTION_REFUSED
1st Issue - Even after running the code properly, why the web page is not displayed
Now, I enter python command in my command prompt and then I try running file again python hello-world.py. Here I am getting the below error
>>> File "<stdin>", line 1
python hello-world.py
^
SyntaxError: invalid syntax
2nd Issue - Why I am getting an error while trying run the hello-world.py
Please guide me on how to resolve this issue.
From what you're describing, I think you're trying to use the Flask Quickstart. If so, have you done the following command in your command prompt enviroment?
C:\path\to\app>set FLASK_APP=hello.py
At which point if you run your code and it's correctly structured you should see
Running on http://127.0.0.1:5000/
Because you don't see the "Running" on your prompt I would assume this hasn't been done. If this doesn't work, I would just double check that Flask is installed and that Python sees it, and that you're following the steps in the tutorial correctly.
Edit: Also, I would recommend removing the hyphen in hello-world.py. It may help you avoid errors here and elsewhere in python coding. See the responses to this question.
1st issue - firewall or some other services may be blocking your port.
2nd Issue - you are running in interactive terminal. Please execute the command in command prompt.
Related
I am launching a Django API from the console via the following command line:
python3.10 manage.py runserver 0.0.0.0:8080
However, sometimes, when there is an error on one of the pages, like for instance if I import a python package that was not installed via pip, the webserver does not get launched, I get a python exception on the console, but no webserver is launched (the network port is not even listening).
Is there a way to still have the webserver running and showing any exceptions or errors that might arise ? This API is for learning purposes, the students should only be able to deploy their code by doing a git push and the new code is deployed. But in case of an error that is not shown in the webpages they would not know what went wrong, they do not have access to the server to see the console.
Thank you for your help.
I am creating a blog using Flask. However, when trying to run my code on a local server, I am unable to do so, as it comes with this error:
Error: Failed to find Flask application or factory in module "flaskblog". Use "FLASK_APP=flaskblog:name to specify one.
I had typed in, "set FLASK_APP=flaskblog.py" in my terminal prior, then typed in "flask run." What would be the next best steps to take so I can run the code on a local server? Running this on a Windows 10 computer.
If you're using a bash cli, try using "export FLASK_APP=flaskblog.py". Set might only work if you are using something like Windows CMD.
I'm running since yesterday a Flask web application on Windows in dev mode through command line : "env FLASK_APP=theflaskapp.py python -m flask run".
Everything was going well untill today, Flask run the same code even if I change some code lines and it keeps running even if I close the terminal. I don't understand why.
Maybe you can help me, please !
I am following this online tutorial to learn Flask with MongoDB. I ran my Flask file in my local machine. with the following command
python connect.py
I don't see any error.
And I wish to see in which path this python file is running. For example, as per the tutorial, the output in the command line shows the following:
Running on http://127.0.0.1:5000/
And in my case, I don't see any such server path which I can go and see, for further development.
Can anyone please let me know how I can get to see this path in my machine?
When you run connect.py, the directory of this file acts as the root of the website. anything after / will be in the same folder as connect.py. When you run it, it will create a new socket which, by default, you can access by going to your browser and navigating to http://127.0.0.1:5000/ this can be changed by putting a HOST and or PORT argument inside of where the app is started, usually in app.run() as most tutorials will use as an example, like so.
if __name__ == '__main__':
app.run(host="192.168.1.11", port=8080, debug=True)
http://192.168.1.11:8080/
I am learning Flask.
I was able to run the Hello World tutorial as shown here
Then I tried to build the Flaskr program following the tutorial http://flask.pocoo.org/docs/tutorial/introduction/
I ran into an issue with the Flaskr program accessing the database,specifically "sqlite3.OperationalError
OperationalError: unable to open database file"
so I took a break and went back to seeing if I could run my "Hello World" program.
Now when I go to the url 127.0.0.1:5000/, instead of seeing "hello world" I still see my data base error from the Flaskr program.
It seem like I need to reset the server instance or something? Please help!
kill python task in the task-manager and then run your server
If you're testing or working on multiple projects at the same time, please run each one in a dedicated virtual environment and serve at a different port because by default flask serves at 127.0.0.1:5000.
Use something like this below:
if __name__ == "__main__":
app.run(host='0.0.0.0',port=8001)
You can change the port in each other project and run all of them without any problem.
Happy coding,
J.