flaskwebgui
flaskwebgui copied to clipboard
flaskwebgui is getting killed immediately
Hi, I have just created an single page flask web app all it does is say hello world here is the program
# gui.py
from flask import Flask
from flaskwebgui import FlaskUI
from os import path
b_path = path.join(path.dirname(__file__), 'Browsor', 'chrlauncher.exe')
app = Flask(__name__)
ui = FlaskUI(app, fullscreen=True, start_server='flask',browser_path= b_path)
@app.get('/')
def home():
return "Hello World"
@app.get('/page')
def page():
return '''
This is Another Page
<a href="/">Go To Home page</a>
'''
if __name__ == '__main__':
ui.run()
when i run this file python3 gui.py
it start chromium and program ends after that, which is why it shows

and this is on terminal
and i am using this chromium version
Turns out it only work for the first time, When i restart my pc and download fresh copy of chromium
after that when i run my gui.py it works fine but if i click on exit button or do ctrl+c in terminal it shutdown and this problem occur whenever i run gui.py again
its some port issue here take a look

Note: in above image it says port 5000
This is when app is working

Note: in above image it says port 5001
This is when app isn't working
i tried to add self.tried_ports.remove(self.port) in line 329 just above logging.info("Closing connections...")
but then app wont close
-
Delete flaskwebgui folder from
tempfolder src, that's where chrome settings are saved; -
If port
5000is occupied another port will be used (that's way you see 5001);
Deleting flaskwebgui folder from temp will probably fix the issue.
There is no folder named flaskwebgui inside my tmp folder

Does it work if you set app_mode=False?
it does work but then it opens in browser edge for me infact i added
import webbrowser as wb
wb.open_new('http://localhost:5000')
app.run
and it works same
One of these conditions evaluate to True see here.
Some chrome flags are experimental, not sure if they are turned off on edge or other chromium based browsers.
Does it work with Chrome browser installed?
gui_running is true this line
so i tried adding self.BROWSER_PROCESS.kill() in last but it seems to pause at this line
i tested all of the flags are working with chromium ( fullscreen and maximized dont with edge but still it opens in app_mode)
If gui_running is true, server should NOT close, if it's false (you closed chrome window) then while loop ends and server is closed.
Chrome window/gui is closed manually by the user you don't need self.BROWSER_PROCESS.kill(). Only the server should close if gui is closed.
while True:
gui_running = psutil.Process(self.BROWSER_PROCESS.pid).is_running()
gui_memory_usage = psutil.Process(self.BROWSER_PROCESS.pid).memory_percent()
if (
gui_running == False
or
gui_memory_usage == 0
): break
time.sleep(5)
Server prematurely closes because if condition is met. You either closed the gui or gui_running/gui_memory_usage give false information (which is a psutil issue).
Are you using a Debian distro? (Ubuntu, lubuntu etc)
i am using Ubuntu on wsl(windows subsystem for linux)
and i have noticed that chromium doesn't close by just pressing cross button
you will have to go to tool bar and close it manually
i kinda solved this immediate closing issue by putting and in place of or in if statement
but now even tho i click on cross it doesn't close
Start flaskwebgui without wsl, use the simple windows cmd.
it works on simple windows cmd
i mean now i can open gui and it doesnt show error
but it wont close even if i press ctrl+c or i click on close button in chromium or even in chrome
here is error
flaskwebgui - [ERROR] - Could not use psutil to check if gui is opened!
That is a psutil issue, remove the try/except block and see where it fails (it may be a permission error).
It's weird that the window doesn't close when you click close (X).
Ya There is some error in line 304
i added bunch of print statement and it seems that program doesn't execute after line 304
i guess there is some error psutil which it handle by terminating program, therefore it don't execute any further
It 'hangs' on line 304? Code bellow line 304 isn't executed anymore?
yep code bellow line 304 isn't executing at least not for me
I tested it on 2 other pc and it seems that its getting hang at line 304 because even if they click close program wont close terminate
Try an old version and see if this works better for you: https://pypi.org/project/flaskwebgui/0.1.13/
I had no issues on ubuntu. Will try to test on windows when I have some time.

is it chromium ?
Chrome, but should be the same for all chromium based browsers.
Version 0.3.0 should fix this issue.
As we can find at flaskwebgui documentation is required to create an additional configuration in order to keep alive de process during the UI execution time.
Inside your static folder you should create a .js file with the following config:
App Path/static/js/keep.alive.js
async function getRequest(url='') {
const response = await fetch(url, {
method: 'GET',
cache: 'no-cache'
})
return response.json()
}
document.addEventListener('DOMContentLoaded', function() {
let url = document.location
let route = "/flaskwebgui-keep-server-alive"
let interval_request = 3 * 1000 //sec
function keep_alive_server(){
getRequest(url + route)
.then(data => console.log(data))
}
setInterval(keep_alive_server, interval_request)()
})
Inside every HTML file used by the UI you should add the following lines:
<script src="{{ url_for('static', filename='js/keep.alive.js')}}"></script>
An that's it!! Hope this work for you!
i have the same problem with an .exe in "one file" using pyinstaller it open the edge browser too late and when the window of the browser opens the console prompt is closing the server, in my case i using windows and flask-appbuilder and it takes an time charging the routes, if i generate the .exe with pyinstaller in "one directory" works but i dont know why fails in "one file"
If you add the keep alive js script it does the same?
If you add the keep alive js script it does the same?
yea the window of the browser opens too late, so the console closes in that waiting time
Change the idle_interval to 50.
If you have any issues with the app closing prematurly set close_server_on_exit parameter to False.