Flask application in spyder - python

I am trying to run a simple flask application in spyder(anaconda). But I am not getting expected results. Can anyone help me here?
from flask import Flask
app = Flask(__name__)
def return_val(a):
return a+1
#app.route('/')
def index():
return return_val(8)
if __name__ == "__main__":
app.run(port=8080)
I am expecting 9 to be printed on the application.

Related

Getting Error 500 _____ after setting up basic Python Flask app with cPanel

I'm attempting to host a basic python app on a domain that I purchased through Namecheap and it's being hosted on cPanel. I've followed this tutorial word for word, however I'm receiving this 500 error (image below) and I can't seem to figure it out. Anyone have any suggestions on how to solve this issue? Thank you!
This is my app.py file:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
#app.route("/<string:name>/")
def say_hello(name):
return "Hello {name}!"
if __name__ == "__main__":
app.run()
This is my passenger_wsgi.py file:
from app import app as application

How to redirect in aws app runner with flask

I am trying to deploy my flask application to aws app runner, locally everything works perfectly. But I can't figure out how to redirect to another page of my website in app runner
My code looks similar to this
from flask import Flask, url_for
from waitress import serve
app = Flask(__name__)
#app.route("/hello")
def hello():
return "Hello"
#app.route("/redirect_to")
def redirect_to():
return "Redirected successfully!"
#app.route("/redirect_from")
def redirect_from():
return redirect(url_for("redirect_to"))
if __name__ == "__main__":
serve(app, host="0.0.0.0", port=8000)
App runner provided "Default domain" that redirects all traffic to my app, that is running on 0.0.0.0:8000. When I request default-domain.awsapprunner.com/hello, it successfully redirects to 0.0.0.0:8000/hello, but when I try to request default-domain.awsapprunner.com/redirect_from page loads forever. I think it happens because my app redirects to 0.0.0.0, and app runner expects that all traffic comes to default-domain.awsapprunner.com but I am not sure
What is the best way to fix this problem?
from flask import Flask, url_for, redirect
from waitress import serve
app = Flask(__name__)
#app.route("/hello")
def hello():
return "Hello"
#app.route("/redirect_to")
def redirect_to():
return "Redirected successfully!"
#app.route("/redirect_from")
def redirect_from():
return redirect("http://YOUR_APP_URL.com/redirect_to")
if __name__ == "__main__":
serve(app, host="0.0.0.0", port=8000)

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

python code doesn't work when I segregate into packages | works fine in same file

I am trying to build some restful API's. When I try to segregate code into packages the service doesn't work and I get URL not found on the server. For examples:
Scenario 1 [Works fine as I have everything in main.py]
from flask import Flask, jsonify, request
app = Flask(__name__)
#app.route('/echo', methods=['POST'])
def echo():
message = request.get_json().get('message', '')
return jsonify({'message': message})
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
Now when I try to segregate the code into different packages, it just doesn't work. For example:
Scenario 2 [Doesn't work as the code is in different packages]
I am initializing the app in api/restful.py
from flask import Flask, jsonify, request
app = Flask(__name__)
Then created a service in api/endpoints/service.py
from api.restplus import app, jsonify, request
#app.route('/echo', methods=['POST'])
def echo():
message = request.get_json().get('message', '')
return jsonify({'message': message})
Finally in main.py
from api.restplus import app
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
It seems like the service is not visible to the app when I put it in a different package. Please advise.
Assuming that the issue you get is that flask does not see your service it looks like nothing is importing your service code once you split your code.
Simply modify your main.py file to look like this to fix it:
from api.restplus import app
import api.endpoints.service
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
Hope this helps !
You may want to do this way Or I would suggest, if there are less routes try to have everthing in one file.
from yourfile import app
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
In yourfile.py
from flask import Flask, jsonify, request
app = Flask(__name__)
#app.route('/echo', methods=['POST'])
def echo():
message = request.get_json().get('message', '')
return jsonify({'message': message})

Categories

Resources