Simple Flask Application Not Working - python

I trained a machine learning model which I wanted to deploy as an app. I learnt that flask is especially suited for this.
I have two functions, get_data from the user of the web app and then, infer_results which prints the results of image type.
I am trying to set up flask for the above use case. I started by following this tutorial: https://sourcedexter.com/python-rest-api-flask/
What I did:
In [71]: app = Flask(__name__)
In [72]: #app.route("/me", methods=["GET"])
...: def get_results():
...: return "Dummy Result"
And then,
In [73]: app.run(host="0.0.0.0", threaded=True)
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
But all I get is:
I can't figure out what is going wrong? This is my main question.
On a sidenote, it would be really great if you could offer some advice or suggestion on : how should I go about building my app: I want to do in Python only. But then, how should I design the UI: where user can put his data and upload it? Is there a way to package all my code (the machine learning code + the user input/output) in a desktop application that user can download and run on his PC?

Instead of app.run(host="0.0.0.0", threaded=True)
Use:
app.run(host="localhost", threaded=True)
Or Execute the below script:
from flask import Flask, render_template
app = Flask(__name__)
# index
#app.route('/')
def index():
return "Hello"
# /me
#app.route("/me", methods=["GET"])
def get_results():
return "Dummy Result"
if __name__ == "__main__":
app.run()

Related

Flask standard input from frontend

i have a little Flask app inside a docker.
I want that my flask app auth with my bank account, therefore a package (dkb_robo) is provided and its nice.
But my Bankaccount requiers a TAN from my mobile phone i need to pass through to standart input. dkb_robo somewhere ask with self.dkb_br["tan"] = input("TAN: ")
I want to pass this TAN from the frontend of a flask site. Any idea how to start here?
While googling i just found stuff like this but its aint working :(
app = Flask(__name__)
#app.route('/')
def tutorialspoint():
sys.stdin = StringIO.StringIO('123456')
return 'Hello asasas'
#app.route('/login/')
def tutorialspoint1():
with DKBRobo("username", "password", True, False) as dkb:
print(dkb.last_login)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

How to host publicly visible flask server on windows

I am trying to host a flask server from my windows computer so I can access it from external devices
I am using Flask/Python and have already tried a few things but can't get it to work
Tried running it on 0.0.0.0, port 33, 5000, etc. but I still can't access it this way
from flask import Flask, request, abort
app = Flask(__name__)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=33)
When I then run the file I get:
Running on http://0.0.0.0:33/ (Press CTRL+C to quit)
But it isn't even running there, nor on any other way I can access it
I expect to be able to access my flask application and send requests to it by using my public IP address
What can I do here to make it work?
You have missed an important line in your code:
After the line
app = Flask(__name__)
You have to write the line:
#app.route('/')
We use the route() decorator to tell Flask what URL should trigger our function.
And then define a function that will tell what task to be performed in the web app hosted in the respective address.
The function might look something like this:
def hello_world():
return 'Hello, World!'
The complete code then will look like:
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=33)
Hope this helps.

How do you make flask execute a function via the app.run command?

I have been trying to follow the tutorials to get flask apps to run on Heroku, like this one: https://dev.to/emcain/how-to-set-up-a-twitter-bot-with-python-and-heroku-1n39.
They all tell you to put this in your code in a file server.py:
from flask import Flask
app = Flask(__name__)
app.run(host='0.0.0.0')
And then run the app via the following command:
python3 server.py
But the tutorials don't explain how to connect the actual function you want to run using the app. In my case, I have a File testbot.py that has the function test(arg1) that contains the code I want to execute:
def test(arg1):
while(1):
#do stuff with arg1 on twitter
I want to do something like this:
from flask import Flask
from testbot import test
from threading import Thread
app = Flask(__name__)
app.addfunction(test(arg1='hardcodedparameter'))
app.run(host='0.0.0.0')
So that when the app runs my test() function executes with the argument. Right now my server is starting, but nothing is happening.
Am I thinking about this correctly?
*Edit: I got it working with the solution, so my server.py now looks like this:
from flask import Flask
from testbot import test
def main_process():
test("hardcodeparam")
app = Flask(__name__)
Thread(target=main_process).start()
app.run(debug=True,host='0.0.0.0')
And now test runs as expected.
Before app.run, register the function with a path, e.g.
#app.route('/')
def test(): # no argument
... do one iteration
return 'ok'
Then visiting the URL will trigger the function. Sites such as https://cron-job.org/ can automate that visiting on a regular basis for free, as suggested here.
If the regular intervals aren't good enough, then you could try:
#app.route('/')
def index(): # no argument
return 'ok'
def test():
while True:
# do stuff
from threading import Thread
Thread(target=test).start()
app.run(...)
You will probably still need to have a job regularly visiting the URL so that Heroku sees that the server is alive and in use.

Python script with flask server takes too long to exit on Windows

I'm new to Python.
As a part of a project I'm trying to deploy a Flask server locally, through the Windows command line.
My Python version is 3.6.0.
The code:
from flask import Flask
app = Flask(__name__)
#app.route('/') def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run()
The problem:
It's about killing the script as it runs. Launching this script with python deploy.py and hitting CTRL+C shuts it off.
BUT - if I hit access that '/' route via the browser once or more, and a moment later try to kill the script in the same manner, then it would take about 10 seconds of nothing until it responds and is finally killed.
Why is this happening? How can I shut the server off immediately each time for continuous and quick development?
Thanks!!
Well if your goal is continuous and quick development, then you can change flask's configuration.
Best solution for your problem would be setting the DEBUG setting to True. If DEBUG is set to True, then flask will automatically reload the server on code changes.
There are a few ways to do this but the easiest one(because you said you are a beginner) is to pass the debug argument to app.run()
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run(debug=True)

Using requests module in flask route function

Consider the following minimal working flask app:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "I am /"
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
This happily works. But when I try to make a GET request with the "requests" module from the hello route to the api route - I never get a response in the browser when trying to access http://127.0.0.1:5000/
from flask import Flask
import requests
app = Flask(__name__)
#app.route("/")
def hello():
r = requests.get("http://127.0.0.1:5000/api")
return "I am /" # This never happens :(
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
So my questions are: Why does this happen and how can I fix this?
You are running your WSGI app with the Flask test server, which by default uses a single thread to handle requests. So when your one request thread tries to call back into the same server, it is still busy trying to handle that one request.
You'll need to enable threading:
if __name__ == "__main__":
app.run(threaded=True)
or use a more advanced WSGI server; see Deployment Options.

Categories