How to run python flask app and webview simultaneosuly - python

I am trying to run flask app and webview simultaneously but it seems that only flask app is running first and blocking the webview to open it.
if __name__ == '__main__':
os.system('python app.py')
webview.create_window('Hello world', 'http://127.0.0.1:5000/')
webview.start()
Webview command is only initiated once I close the flask server (Ctr+C) but till then webview returns connection refused.

You need to run your flask app in the background. Here is a sample working example. os.system('python app.py &')
main.py
import webview
import os
if __name__ == '__main__':
os.system('python app.py &')
webview.create_window('Hello world', 'http://127.0.0.1:5000/')
webview.start()
app.py
# Importing flask module in the project is mandatory
# An object of Flask class is our WSGI application.
from flask import Flask
# Flask constructor takes the name of
# current module (__name__) as argument.
app = Flask(__name__)
# The route() function of the Flask class is a decorator,
# which tells the application which URL should call
# the associated function.
#app.route('/')
# ‘/’ URL is bound with hello_world() function.
def hello_world():
return 'Hello World'
# main driver function
if __name__ == '__main__':
# run() method of Flask class runs the application
# on the local development server.
app.run()

Related

How to use Python thread with Flask in Apache

I'm trying to deploy my Flask app to Apache webserver.
In my main file, before running the Flask app, I start a daemon thread that runs in background:
# ... here there are all the imports and the Flaks routes
def main():
x = threading.Thread(target=my_thread_function)
x.daemon = True
x.start()
# app.run() # I moved this line below
x.join()
if __name__ == '__main__':
main()
app.run()
This is my wsgi file:
import sys
sys.path.insert(0, 'C:\\myapp\\')
from myapp import app as application
If I run Apache server, Flask works fine and I can see the web app interface when I connect to the IP address. However, the background thread doesn't work, because I don't see its "effect" in the app.
If I run the same code with the Flask development server, the background thread works fine.
How can it work with Apache?
remove the join() because it will block the app.run()
# ... here there are all the imports and the Flaks routes
def main():
x = threading.Thread(target=my_thread_function)
x.daemon = True
x.start()
if __name__ == '__main__':
main()
app.run()

Schedule a function at particular time of day using a flask app

I want to schedule (every day at 17:00) a single function in a flask app (my flask app has multiple functions). How can I do that?
from flask import Flask
import schedule
import time
app = Flask(__name__)
app.secret_key = 'my precious'
def fct1():
print("bla bla bla")
def myfunction():
print("aaaaaaaaaaaaaaaa")
def programare():
schedule.every().day.at("17:00").do(fct1)
while True:
schedule.run_pending()
time.sleep(1)
if __name__== '__main__':
app.run(debug=True)
Simple thread if server startted standalone:
if __name__== '__main__':
import threading
threading.Thread(target=programare).start()
app.run(debug=True)
If server deployed with wsgi or etc I suggest to run shedule separately.

waitress command line returning "Malformed Application" when deploying flask web app

I'm attempting to deploy a simple web app, and I'm using command line waitress-serve --call command. But every time, the command immediately returns 1. Malformed application 'name_of_project_here'.
Here's my flask web app in python:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run(debug = True)
and the command I run is just
waitress-serve --call "name_of_project"
I did try looking through the documentation, and I found where the error occurs, but couldn't find an explanation of why it's occurring. What does malformed application mean?
Minimal working example:
If you put code in file main.py
from flask import Flask
def create_app():
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World!"
return app
if __name__ == '__main__':
app = create_app()
app.run()
then you can run it as
waitress-serve --call "main:create_app"
So "name_of_project" has to be "filename:function_name" which creates Flask() instance.
It can't be any text. And if you forget : then you may see "Malformed application"
when defining your appname and the function that initialize your app with : dont put them into quotes('')

Flask hangs the main thread

I want to use the Flask as my RESTful API server, but the main thread hangs and it doesn't execute the code after the app.run().
In util/restAPI.py
from flask import Flask
app = Flask('__name__')
#app.route('/')
def index():
return "Hello, World!"
In main.py
from util import restAPI
if __name__ == "__main__":
restAPI.app.run()
print "haha"
Should I use threads or something else to help me?

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