:github_url: https://github.com/kivymd/KivyMD/blob/master/kivymd/uix/filemanager/filemanager.py

FileManager
===========

.. py:module:: kivymd.uix.filemanager.filemanager

.. autoapi-nested-parse::

   Components/FileManager
   ======================

   A simple manager for selecting directories and files.

   Usage
   -----

   .. code-block:: python

       path = os.path.expanduser("~")  # path to the directory that will be opened in the file manager
       file_manager = MDFileManager(
           exit_manager=self.exit_manager,  # function called when the user reaches directory tree root
           select_path=self.select_path,  # function called when selecting a file/directory
       )
       file_manager.show(path)

   .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager.png
       :align: center

   .. warning:: Be careful! To use the `'/'` path on Android devices, you need
       special permissions. Therefore, you are likely to get an error.

   Or with ``preview`` mode:

   .. code-block:: python

       file_manager = MDFileManager(
           exit_manager=self.exit_manager,
           select_path=self.select_path,
           preview=True,
       )

   .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-preview.png
       :align: center

   .. warning:: The `preview` mode is intended only for viewing images and will
       not display other types of files.

   Example
   -------

   .. tabs::

       .. tab:: Declarative Python style with KV

           .. code-block:: python

               import os

               from kivy.core.window import Window
               from kivy.lang import Builder
               from kivy.metrics import dp

               from kivymd.app import MDApp
               from kivymd.uix.filemanager import MDFileManager
               from kivymd.uix.snackbar import MDSnackbar, MDSnackbarText

               KV = '''
               MDScreen:
                   md_bg_color: self.theme_cls.backgroundColor

                   MDButton:
                       pos_hint: {"center_x": .5, "center_y": .5}
                       on_release: app.file_manager_open()

                       MDButtonText:
                           text: "Open manager"
               '''


               class Example(MDApp):
                   def __init__(self, **kwargs):
                       super().__init__(**kwargs)
                       Window.bind(on_keyboard=self.events)
                       self.manager_open = False
                       self.file_manager = MDFileManager(
                           exit_manager=self.exit_manager, select_path=self.select_path
                       )

                   def build(self):
                       self.theme_cls.theme_style = "Dark"
                       return Builder.load_string(KV)

                   def file_manager_open(self):
                       self.file_manager.show(
                           os.path.expanduser("~"))  # output manager to the screen
                       self.manager_open = True

                   def select_path(self, path: str):
                       '''
                       It will be called when you click on the file name
                       or the catalog selection button.

                       :param path: path to the selected directory or file;
                       '''

                       self.exit_manager()
                       MDSnackbar(
                           MDSnackbarText(
                               text=path,
                           ),
                           y=dp(24),
                           pos_hint={"center_x": 0.5},
                           size_hint_x=0.8,
                       ).open()

                   def exit_manager(self, *args):
                       '''Called when the user reaches the root of the directory tree.'''

                       self.manager_open = False
                       self.file_manager.close()

                   def events(self, instance, keyboard, keycode, text, modifiers):
                       '''Called when buttons are pressed on the mobile device.'''

                       if keyboard in (1001, 27):
                           if self.manager_open:
                               self.file_manager.back()
                       return True


               Example().run()

       .. tab:: Declarative python style

           .. code-block:: python

               import os

               from kivy.core.window import Window
               from kivy.metrics import dp

               from kivymd.app import MDApp
               from kivymd.uix.button import MDButton, MDButtonText
               from kivymd.uix.filemanager import MDFileManager
               from kivymd.uix.screen import MDScreen
               from kivymd.uix.snackbar import MDSnackbar, MDSnackbarText


               class Example(MDApp):
                   def __init__(self, **kwargs):
                       super().__init__(**kwargs)
                       Window.bind(on_keyboard=self.events)
                       self.manager_open = False
                       self.file_manager = MDFileManager(
                           exit_manager=self.exit_manager, select_path=self.select_path
                       )

                   def build(self):
                       self.theme_cls.theme_style = "Dark"
                       return (
                           MDScreen(
                               MDButton(
                                   MDButtonText(
                                       text="Open manager"
                                   ),
                                   pos_hint={"center_x": .5, "center_y": .5},
                                   on_release=self.file_manager_open,
                               ),
                               md_bg_color=self.theme_cls.backgroundColor
                           )
                       )

                   def file_manager_open(self, *args):
                       self.file_manager.show(
                           os.path.expanduser("~"))  # output manager to the screen
                       self.manager_open = True

                   def select_path(self, path: str):
                       '''
                       It will be called when you click on the file name
                       or the catalog selection button.

                       :param path: path to the selected directory or file;
                       '''

                       self.exit_manager()
                       MDSnackbar(
                           MDSnackbarText(
                               text=path,
                           ),
                           y=dp(24),
                           pos_hint={"center_x": 0.5},
                           size_hint_x=0.8,
                       ).open()

                   def exit_manager(self, *args):
                       '''Called when the user reaches the root of the directory tree.'''

                       self.manager_open = False
                       self.file_manager.close()

                   def events(self, instance, keyboard, keycode, text, modifiers):
                       '''Called when buttons are pressed on the mobile device.'''

                       if keyboard in (1001, 27):
                           if self.manager_open:
                               self.file_manager.back()
                       return True


               Example().run()

   .. versionadded:: 1.0.0

   Added a feature that allows you to show the available disks first, then the
   files contained in them. Works correctly on: `Windows`, `Linux`, `OSX`, `Android`.
   Not tested on `iOS`.

   .. code-block:: python

       def file_manager_open(self):
           self.file_manager.show_disks()

   .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-show-disks.png
       :align: center


API - :mod:`kivymd.uix.filemanager.filemanager`
-----------------------------------------------

.. py:class:: MDFileManager(*args, **kwargs)




   Implements a modal dialog with a file manager.

   For more information, see in the
   :class:`~kivymd.theming.ThemableBehavior` and
   :class:`~kivy.uix.relativelayout.RelativeLayout`
   classes documentation.

   :Events:
       `on_pre_open`:
           Called before the MDFileManager is opened.
       `on_open`:
           Called when the MDFileManager is opened.
       `on_pre_dismiss`:
           Called before the MDFileManager is closed.
       `on_dismiss`:
           Called when the MDFileManager is closed.

   .. py:attribute:: icon

      Icon that will be used on the directory selection button.

      .. deprecated:: 1.1.0
          Use :attr:`icon_selection_button` instead.

      :attr:`icon` is an :class:`~kivy.properties.StringProperty`
      and defaults to `check`.


   .. py:attribute:: icon_selection_button

      Icon that will be used on the directory selection button.

      .. versionadded:: 1.1.0

      .. code-block:: python

          MDFileManager(
              ...
              icon_selection_button="pencil",
          )

      .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-icon-selection-button.png
          :align: center

      :attr:`icon_selection_button` is an :class:`~kivy.properties.StringProperty`
      and defaults to `check`.


   .. py:attribute:: background_color_selection_button

      Background color in (r, g, b, a) or string format of the current
      directory/path selection button.

      .. versionadded:: 1.1.0

      .. code-block:: python

          MDFileManager(
              ...
              background_color_selection_button="brown",
          )

      .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-background-color-selection-button.png
          :align: center

      :attr:`background_color_selection_button` is an :class:`~kivy.properties.ColorProperty`
      and defaults to `None`.


   .. py:attribute:: background_color_toolbar

      Background color in (r, g, b, a) or string format of the file manager toolbar.

      .. versionadded:: 1.1.0

      .. code-block:: python

          MDFileManager(
              ...
              background_color_toolbar="brown",
          )

      .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-background-color-toolbar.png
          :align: center

      :attr:`background_color_toolbar` is an :class:`~kivy.properties.ColorProperty`
      and defaults to `None`.


   .. py:attribute:: icon_folder

      Icon that will be used for folder icons when using ``preview = True``.

      .. code-block:: python

          MDFileManager(
              ...
              preview=True,
              icon_folder="path/to/icon.png",
          )

      .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-icon-folder.png
          :align: center

      :attr:`icon` is an :class:`~kivy.properties.StringProperty`
      and defaults to `check`.


   .. py:attribute:: icon_color

      Color in (r, g, b, a) or string format of the folder icon when the
      :attr:`preview` property is set to False.

      .. versionadded:: 1.1.0

      .. code-block:: python

          MDFileManager(
              ...
              preview=False,
              icon_color="brown",
          )

      .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-icon-color.png
          :align: center

      :attr:`icon_color` is an :class:`~kivy.properties.ColorProperty`
      and defaults to `None`.


   .. py:attribute:: exit_manager

      Function called when the user reaches directory tree root.

      :attr:`exit_manager` is an :class:`~kivy.properties.ObjectProperty`
      and defaults to `lambda x: None`.


   .. py:attribute:: select_path

      Function, called when selecting a file/directory.

      :attr:`select_path` is an :class:`~kivy.properties.ObjectProperty`
      and defaults to `lambda x: None`.


   .. py:attribute:: ext

      List of file extensions to be displayed in the manager.
      For example, `['.py', '.kv']` - will filter out all files,
      except python scripts and Kv Language.

      :attr:`ext` is an :class:`~kivy.properties.ListProperty`
      and defaults to `[]`.


   .. py:attribute:: search

      It can take the values 'all' 'dirs' 'files' - display only directories
      or only files or both them. By default, it displays folders, and files.
      Available options are: `'all'`, `'dirs'`, `'files'`.

      :attr:`search` is an :class:`~kivy.properties.OptionProperty`
      and defaults to `all`.


   .. py:attribute:: current_path

      Current directory.

      :attr:`current_path` is an :class:`~kivy.properties.StringProperty`
      and defaults to `os.path.expanduser("~")`.


   .. py:attribute:: use_access

      Show access to files and directories.

      :attr:`use_access` is an :class:`~kivy.properties.BooleanProperty`
      and defaults to `True`.


   .. py:attribute:: preview

      Shows only image previews.

      :attr:`preview` is an :class:`~kivy.properties.BooleanProperty`
      and defaults to `False`.


   .. py:attribute:: show_hidden_files

      Shows hidden files.

      :attr:`show_hidden_files` is an :class:`~kivy.properties.BooleanProperty`
      and defaults to `False`.


   .. py:attribute:: sort_by

      It can take the values 'nothing' 'name' 'date' 'size' 'type' - sorts files
      by option. By default, sort by name.
      Available options are: `'nothing'`, `'name'`, `'date'`, `'size'`, `'type'`.

      :attr:`sort_by` is an :class:`~kivy.properties.OptionProperty`
      and defaults to `name`.


   .. py:attribute:: sort_by_desc

      Sort by descending.

      :attr:`sort_by_desc` is an :class:`~kivy.properties.BooleanProperty`
      and defaults to `False`.


   .. py:attribute:: selector

      It can take the values 'any' 'file' 'folder' 'multi'
      By default, any.
      Available options are: `'any'`, `'file'`, `'folder'`, `'multi'`.

      :attr:`selector` is an :class:`~kivy.properties.OptionProperty`
      and defaults to `any`.


   .. py:attribute:: selection

      Contains the list of files that are currently selected.

      :attr:`selection` is a read-only :class:`~kivy.properties.ListProperty`
      and defaults to `[]`.


   .. py:attribute:: selection_button

      The instance of the directory/path selection button.

      .. versionadded:: 1.1.0

      :attr:`selection_button` is a read-only :class:`~kivy.properties.ObjectProperty`
      and defaults to `None`.


   .. py:method:: show_disks() -> None


   .. py:method:: show(path: str) -> None

      Forms the body of a directory tree.

      :param path:
          The path to the directory that will be opened in the file manager.


   .. py:method:: get_access_string(path: str) -> str


   .. py:method:: get_content() -> Union[Tuple[List[str], List[str]], Tuple[None, None]]

      Returns a list of the type [[Folder List], [file list]].


   .. py:method:: close() -> None

      Closes the file manager window.


   .. py:method:: select_dir_or_file(path: str, widget: MDFileManagerItemPreview | MDFileManagerItem) -> None

      Called by tap on the name of the directory or file.


   .. py:method:: back() -> None

      Returning to the branch down in the directory tree.


   .. py:method:: select_directory_on_press_button(*args) -> None

      Called when a click on a floating button.


   .. py:method:: on_icon(instance_file_manager, icon_name: str) -> None

      Called when the :attr:`icon` property is changed.


   .. py:method:: on_background_color_toolbar(instance_file_manager, color: str | list) -> None

      Called when the :attr:`background_color_toolbar` property is changed.


   .. py:method:: on_pre_open(*args) -> None

      Default pre-open event handler.

      .. versionadded:: 1.1.0


   .. py:method:: on_open(*args) -> None

      Default open event handler.

      .. versionadded:: 1.1.0


   .. py:method:: on_pre_dismiss(*args) -> None

      Default pre-dismiss event handler.

      .. versionadded:: 1.1.0


   .. py:method:: on_dismiss(*args) -> None

      Default dismiss event handler.

      .. versionadded:: 1.1.0




