slow response from plyer.filechooser

130 views
Skip to first unread message

Ryan Anderson

unread,
Nov 1, 2022, 6:04:57 PM11/1/22
to Kivy users support
Hi Kivy users,

I'm having a bit of trouble with plyer.filechooser.

It works as expected, but it is VERY slow.  The test application below uses a filechooser to allow the user to browse for a path, then simply updates a label to display the chosen path. Between the time that "Open" is clicked in the filechooser dialog and the time that the label is updated, 9 seconds pass on my machine.  Instrumenting the code with some simple profiling reveals that it is taking those 9 seconds for filechooser.open_file() to return.

Example code is attached.

Does anyone know why this is so slow, and how it can be improved?

I'm using Kivy 2.1.0, plyer 2.0.0, and Python 3.10.7 on Windows 10.

Here's the sample code:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from plyer import filechooser


class TestApp(App):
     def build(self):
         layout = BoxLayout(orientation='vertical')
         button = Button(text='Push to Select File')
         label = Label(text='No File')
         layout.add_widget(button)
         layout.add_widget(label)

         def on_button(*args, **kwargs):
             path = filechooser.open_file(title='Open File')
             if path:
                 label.text = path[0]

         button.bind(on_release=on_button)
         return layout


if __name__ == '__main__':
     TestApp().run()

Thanks in advance for any insight!

test_plyer.py

Elliot Garbus

unread,
Nov 1, 2022, 7:02:18 PM11/1/22
to kivy-...@googlegroups.com

Here are a few suggestions that might address the performance issue you are seeing.

  1. Use the callback on_selection to set the action when a file has been selected, rather than use the returned value. 
  2. Set the path of the directory you want to view.  I have set it to the current working directory.  You may have a different directory you need to set.
  3. You can use the filter attribute to see only the files you are interested in, I doubt this would improve speed, but it does improve the user experience.

 

The plyer filechooser uses the same api as the kivy filechooser: https://kivy.org/doc/stable/api-kivy.uix.filechooser.html?highlight=filechooser#module-kivy.uix.filechooser

 

 

 

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from plyer import filechooser


class TestApp
(App):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.label = None  # the label widget

   
def build(self):
        layout = BoxLayout(
orientation='vertical')
        button = Button(
text='Push to Select File')
       
self.label = Label(text='No File')
        layout.add_widget(button)
        layout.add_widget(
self.label)

       
def on_button(*args):
            filechooser.open_file(
title='Open File', path='.', filters=['*.py'],
                                 
on_selection=self.handle_selection

        
button.bind(on_release=on_button)
       
return layout

   
def handle_selection(self, selection):
       
print(selection)
       
self.label.text = selection[0] if selection else 'No File'


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/fd98bee3-c07d-401c-84d8-03d07126756cn%40googlegroups.com.

 

Ryan Anderson

unread,
Nov 2, 2022, 11:34:02 AM11/2/22
to kivy-...@googlegroups.com
Thanks for your suggestions.

Unfortunately, they made no difference on the return time of the filechooser.open_file() method.  Your version of the code, just like the sample code I sent, updates the label 6-10 seconds after the user clicks the open button on the filechooser dialog (the open_file() call takes just as long if the user clicks cancel instead of open, by the way).

I wonder if this can be replicated on other machines, or if I'm seeing something unique to my environment.  Perhaps the filechooser dialog is using a network resource that is occupying some time upon closing the dialog?  Does anyone have any suggestions for profiling exactly where the time is being used?

Thanks again for the help.

Ryan Anderson



You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/ioV_4WpAYE8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6361a572.170a0220.50ac6.96f1SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Elliot Garbus

unread,
Nov 2, 2022, 11:42:37 AM11/2/22
to kivy-...@googlegroups.com

That is strange.  I wonder if there is an install issue on your machine or something strange in your environment.  The open file dialog opens as soon as I release the mouse button.  I did not experience any delay.

Ryan Anderson

unread,
Nov 2, 2022, 11:48:24 AM11/2/22
to kivy-...@googlegroups.com
I found the source code in plyer/platforms/win/filechooser.py and profiled it.

The delay is coming from the call to win32gui.GetOpenFileNameW().  It appears that, for some reason I don't yet understand, the system file selection dialog itself is taking several seconds to return.  I get the same delay if I just execute:

import win32gui
win32gui.GetOpenFileNameW()

So, it appears that this isn't a Kivy problem, it's a Windows problem.   I still don't know how to fix it, but at least I have some breadcrumbs.

Thanks,

Ryan Anderson



Elliot Garbus

unread,
Nov 2, 2022, 11:50:35 AM11/2/22
to kivy-...@googlegroups.com

FWIW. I’m running Windows 11.  The machines I have tested on are using SSDs.

Ryan Anderson

unread,
Nov 2, 2022, 11:57:39 AM11/2/22
to kivy-...@googlegroups.com
I just came across this stackoverflow post where someone reports identical behavior when trying to use a native file open dialog from QT.  This seems to confirm that it's a network issue local to my machine.


I'll take my search for solutions offline from here.  I appreciate your help!

Ryan Anderson
Software Engineer
  
123 Commercial Drive
Bozeman, MT 59715


Reply all
Reply to author
Forward
0 new messages