Built a new Flask app -- old one still showing in browser - python

I made the following file yesterday.
# import flask
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__)
# create url & function mapping for root or /
#app.route('/')
def index():
return "Hello from Flask"
# create another mapping name /hello
#app.route('/hello')
def hello():
myName = "kayak"
return "Hello again !!" + myName
# create mapping for /myprofile
#app.route('/myprofile')
def showmyprofile():
return render_template('myprofile.html')
# create mapping for /myprofile
#app.route('/addprofileform')
def addprofileform():
return render_template('myprofileform.html')
# create a mapping for /addprofile
#app.route('/addprofile')
def addprofile():
myname = request.args.get('myname')
state_of_residence = request.args.get('state_of_residence')
return render_template('myprofile.html', html_page_name=myname,
html_page_state_of_residence=state_of_residence)
if __name__== '__main__':
app.run()
Then I made the following file today.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'This is the homepage'
if __name__ == "__main__":
app.run(debug=True)
I thought
app.run(debug=True)
would work to clear the old data, but I doesn't and http://127.0.0.1:5000/ page keeps showing "Hello from Flask".
How do I fix this?

Just clear the cache in your browser and try running it again.
Here's how to clear your cache in some browsers:
Firefix->https://support.mozilla.org/en-US/kb/how-clear-firefox-cache
Chrome->https://support.google.com/accounts/answer/32050?co=GENIE.Platform%3DDesktop&hl=en

You can export the FLASK_ENV environment variable and set it to development before running the server
export FLASK_ENV=development
flask run
This worked for me.

Running the program in incognito tab will not cause this error. No need to clear caches also. See https://support.google.com/chrome/answer/95464?co=GENIE.Platform%3DAndroid&hl=en

Related

Python - access flask app context before serving requests

Usecase: I have a python flask app that runs background_function() before serving any requests on routes.
When I execute the flask app, I receive the error - RuntimeError: Working outside of application context. I receive the error since I try to get the application context before any request is served.
What is the best pythonic way to execute the background_function() in this example?
from flask import Flask
from download import Download
app = Flask(__name__)
app.config.from_pyfile('config.py')
# run backgroung function
Download.background_function()
#app.route('/')
def index():
return 'Welcome!'
if __name__ == '__main__':
app.run()
The config file
FILE_LOCATION = os.environ['FILE_LOCATION'] # "file/path/on/server"
# Many other variables are present in this file
The download file
from flask import current_app as app
class Download:
#staticmethod
def background_function():
file_path = app.config["FILE_LOCATION"]
# code to download file from server to local
return
Try this:
from flask import Flask
from download import Download
app = Flask(__name__)
#app.route('/')
def index():
return 'Welcome!'
if __name__ == '__main__':
Download.background_function()
app.run()
the download file
from flask import current_app as app
class Download:
#staticmethod
def background_function():
print("testing")
given output:
testing
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
As you can see, the function runs first and prints testing and then runs the application.

how to use a variable to define a directory flask python3

I have this sample code below, when I try to go to / I get a 404 error, I am trying to use variables as app routes but with no luck
from flask import Flask
app = Flask(__name__)
index_dir = "/"
app.route(index_dir)
def index():
return "hello_world"
if __name__ == '__main__':
app.run(host="0.0.0.0")
Any idea on how to make python3 variables to directories
You have skipped the decorator symbol (#) before app.route(index_dir). It should be like this to work:
from flask import Flask
app = Flask(__name__)
index_dir = "/"
#app.route(index_dir)
def index():
return "hello_world"
if __name__ == '__main__':
app.run(host="0.0.0.0")

It does not recognize the route /greeting

I'm new to flask, I'm trying to do a rest api, but when creating my route it doesn't recognize it for me.
I have imported flask and python 3.8.
from products import products
from flask import Flask
#app.route('/greeting')
def greeting():
return 'hi'
if __name__ == '__main__':
app.run(debug=True, port=4000)
You need to create the instance of the Flask class
app = Flask(__name__)
A minimal Flask application looks something like this:
from flask import Flask
app = Flask(__name__)
#app.route('/greeting')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True, port=4000)
Now you could see it running at:
* Running on http://127.0.0.1:4000/
Access the greeting as http://127.0.0.1:4000/greeting
For more info read this

Flask + Wsgi returning python shell script output

I have deployed two containers flask + wsgi and nginx I have a simple code which works returning hello world.
When I try to return the output of a python shell script to a webpage I get internal server error, the script it works via cli it even prints the output of docker ps.
Working code returns a simple hello world :
# app.py
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')
Not working code i get internal server error please help im not really sure why ... or how to debug it
#!/usr/bin/env python
import subprocess
def dockers():
call = subprocess.call('docker ps', shell=True)
return call
#!/user/bin/env python
from flask import Flask
from cont import dockers
app = Flask(__name__)
print(dockers())
#app.route('/')
def hello_world():
return dockers()
if __name__ == '__main__':
app.run(host='0.0.0.0')
Dont ever try to pass an object to a web page youll have a bad time. i wrote the result into a file split the lines to a list and returned it to the webpage.

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