flaskwebgui icon indicating copy to clipboard operation
flaskwebgui copied to clipboard

flaskwebgui is getting killed immediately

Open itsDrac opened this issue 4 years ago • 27 comments

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 Screenshot (71)

and this is on terminal Capture

and i am using this chromium version

itsDrac avatar May 01 '21 12:05 itsDrac

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

itsDrac avatar May 03 '21 14:05 itsDrac

its some port issue here take a look Screenshot (72)

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

Screenshot (73)

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

itsDrac avatar May 03 '21 18:05 itsDrac

  • Delete flaskwebgui folder from temp folder src, that's where chrome settings are saved;

  • If port 5000 is occupied another port will be used (that's way you see 5001);

Deleting flaskwebgui folder from temp will probably fix the issue.

ClimenteA avatar May 04 '21 06:05 ClimenteA

There is no folder named flaskwebgui inside my tmp folder Screenshot (75)

itsDrac avatar May 04 '21 14:05 itsDrac

Does it work if you set app_mode=False?

ClimenteA avatar May 04 '21 15:05 ClimenteA

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

itsDrac avatar May 05 '21 10:05 itsDrac

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?

ClimenteA avatar May 05 '21 11:05 ClimenteA

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)

itsDrac avatar May 06 '21 13:05 itsDrac

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)

ClimenteA avatar May 07 '21 15:05 ClimenteA

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
Capture1

itsDrac avatar May 07 '21 18:05 itsDrac

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

itsDrac avatar May 07 '21 18:05 itsDrac

Start flaskwebgui without wsl, use the simple windows cmd.

ClimenteA avatar May 08 '21 05:05 ClimenteA

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!

itsDrac avatar May 08 '21 20:05 itsDrac

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).

ClimenteA avatar May 09 '21 06:05 ClimenteA

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

itsDrac avatar May 09 '21 09:05 itsDrac

It 'hangs' on line 304? Code bellow line 304 isn't executed anymore?

ClimenteA avatar May 09 '21 09:05 ClimenteA

yep code bellow line 304 isn't executing at least not for me

itsDrac avatar May 09 '21 20:05 itsDrac

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

itsDrac avatar May 14 '21 17:05 itsDrac

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. djangostart

ClimenteA avatar May 15 '21 07:05 ClimenteA

is it chromium ?

itsDrac avatar May 15 '21 14:05 itsDrac

Chrome, but should be the same for all chromium based browsers.

ClimenteA avatar May 15 '21 15:05 ClimenteA

Version 0.3.0 should fix this issue.

ClimenteA avatar Jun 14 '21 18:06 ClimenteA

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!

davo456 avatar Aug 03 '22 17:08 davo456

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"

EnriqueGautoSand avatar Aug 22 '22 19:08 EnriqueGautoSand

If you add the keep alive js script it does the same?

ClimenteA avatar Aug 23 '22 05:08 ClimenteA

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

EnriqueGautoSand avatar Aug 23 '22 13:08 EnriqueGautoSand

Change the idle_interval to 50.

ClimenteA avatar Aug 23 '22 14:08 ClimenteA

If you have any issues with the app closing prematurly set close_server_on_exit parameter to False.

ClimenteA avatar Sep 22 '22 10:09 ClimenteA

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. djangostart djangostart

in windows it worked for me

mahnamnasir avatar Jul 06 '24 09:07 mahnamnasir