Pycharm Unexpected Result Output - python

I'm a beginner at Pycharm. I'm using Flask web framework to develop a basic web application. I have written a simple code to display "Hello" on my browser, which it did. Strangely, when I add something to 'Hello', such as 'Hello my name is Yusef' and re-run the program; it won't show any changes, it still appears with message 'Hello' on my browser. Any idea, what I'm missing?
Below is my code:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return "hello world"
if __name__ == "__main__":
app.run()

You need to clear the browser cache, this isn't a Pycharm issue, just clear your browser cache and you should be fine.
Open in incognito mode to avoid such issues.

Related

I can't run the Python Flask application, help needed

When I run this Flask application logs in console seem to be fine, but I cannot find my webpage by the default url.
Error: Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. I entered this URL : http://127.0.0.1:5000/ with the trailing slash.
Any thoughts?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return "Hello, World! <h1>Hello, World!<h1>"
if __name__ == '__main__':
app.run(debug = False)
The error webpage looks like this
As you can see in the screenshot, the webserver is running. Just go to your browser and type in the search bar:
localhost:5000

Flask hello world app not working in Spyder or Canopy

I have the following Flask app, and it is returning a 404 error. I have tried it in Spyder and Enthought Canopy, and have also used the
set FLASK_APP
and
flask run
commands at cmd, with Windows 10.
from flask import Flask
app = Flask(__name__)
#app.route("/admin")
def hello_world():
return "Hello World"
if __name__ == '__main__':
app.run(host="localhost", port=int("5000"))
I figured out whats wrong with your code.
You only have declared an admin route.
Accessing localhost:5000/admin gives you your desired output!
try changing#app.route('/admin') to #app.route('/')

Open browser automatically when Python code is executed

I am implementing a GUI in Python/Flask.
The way flask is designed, the local host along with the port number has to be "manually" opened.
Is there a way to automate it so that upon running the code, browser(local host) is automatically opened?
I tried using webbrowser package but it opens the webpage after the session is killed.
I also looked at the following posts but they are going over my head.
Shell script opening flask powered webpage opens two windows
python webbrowser.open(url)
Problem occurs when html pages are rendered based on user inputs.
Thanks in advance.
import webbrowser
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
webbrowser.open_new('http://127.0.0.1:2000/')
app.run(port=2000)
Use timer to start new thread to open web browser.
import webbrowser
from threading import Timer
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
def open_browser():
webbrowser.open_new("http://127.0.0.1:5000")
if __name__ == "__main__":
Timer(1, open_browser).start()
app.run(port=2000)
I'd suggest the following improvement to allow for loading of the browser when in debug mode:
Inspired by this answer, will only load the browser on the first run...
def main():
# The reloader has not yet run - open the browser
if not os.environ.get("WERKZEUG_RUN_MAIN"):
webbrowser.open_new('http://127.0.0.1:2000/')
# Otherwise, continue as normal
app.run(host="127.0.0.1", port=2000)
if __name__ == '__main__':
main()
https://stackoverflow.com/a/9476701/10521959

Start function on run with Flask

I have a flask app
#app.route("/hello")
def generater():
return "hello world"
if __name__ == '__main__':
app.run()
My application runs fine, but i would like to know how I could make a request to http://127.0.0.1:5000/hello when I compile my code
You can use webbrowser to automatically open http://localhost:5000 in a web browser when running your flask app:
import webbrowser
...
if __name__ == '__main__':
webbrowser.open('http://localhost:5000')
app.run()
There are a lot of ways you could do this. You could just open up your browser to that location. You could try #jimtodd's answer and then cURL the endpoint from another terminal window.
To do it in the code, which I guess is what you want, Flask offers you some helper methods. For example there is: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_first_request
You can use it like this:
def foo():
pass
app.before_first_request(foo)
In the case where you want to run a script strictly on run, not just before the first request, this solution is good: Run code after flask application has started -- I guess you would use this for cold-start problems as well.
You can do this from command prompt:
set FLASK_APP=hello.py
python -m flask run
The you will see....
Running on http://127.0.0.1:5000
Now you can check the output in your browser.

Python, Flask, 'Hello World': No browser reaction

I have pip-installed Flask and HTML5 on my Window-system. When I start the Hello World!-program with IDLE, I get a red message in the Python-Shell:
"* Running on xxxx://127.0.0.1:5000/". (xxxx = http)
And when I start it with app.run(debug=True) another red message appears:
"* Restarting with reloader".
My browser (Firefox) shows no reaction.
What can I do to get 'Hello World' in a new tab of Firefox?
The Code is:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
return and app.run are indended
You have to open a new tab with this url:
http://127.0.0.1:5000/
You need to actually open the page in your browser - it won't open itself. Open Firefox and navigate to
127.0.0.1:5000
(it's a URL)
When you run your code, it sits around waiting for a request from the user. When it gets a request, it'll return a response, and that's (sort of) what you see in your browser. Going to a URL is how you send that request - Flask will interpret anything sent to 127.0.0.1:5000 as a request, and try to match the URL to one of your #app.route decorators. For example, if you were to have a function decorated with #app.route("/hello"), then when you go to 127.0.0.1:5000/hello, Flask would run that function to determine the response.
Try out this code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
refrence Flask at first run: Do not use the development server in a production environment
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Try this, this works for me. Open your firefox browser and go to the address given in the output. ex: http://XXXX.X.X.X:5000/

Categories

Resources