[PYTHON] GUI application by Kivy (including matplotlib)

This time, I tried to distribute an application that implements the function to display graphs with GUI by Kivy. I got caught a little, so as a memorandum. Environment is Windows 10

1. Import required libraries

Import Kivy-Garden to enable matplotlib in Kivy. Later, it will be converted to an exe with pyinstaller, but at that time it seems that the garden directory will be lost, so Install matplotlib using --kivy.

$ pip install kivy-garden
$ garden install matplotlib --kivy

2. Make an exe with pyinstaller

Install only the minimum required libraries with pip in a virtual environment. (Omitted for anaconda, pyenv, and virtual environment construction) If there are many unnecessary things, a huge exe file of 200 to 300 Mb will be born ...

$ pip install pyinstaller pypiwin32 #These two are essential

#These guys don't follow the kivy installation so don't forget if you haven't done so.
$ pip install kivy_deps.sdl2 kivy_deps.glew 
$pip install required libraries#All required libraries for py files

3. Packaging

I'm ready. Move your working directory to a python file and do the following: pyinstaller has various useful packaging functions, but here Use --onefile to combine files into one.

$ pyinstaller ***.py --onefile

Various things are generated in the working directory, but for the time being, an exe file is generated without dist. In this state, the GUI does not start yet.

#In the working directory
***.py
***.kv
***.spec
dist
pycache
build

4. Edit spec file

Next, describe the dependencies in the SPEC file to enable the GUI.

#Below spec file
> ***.spec

from kivy_deps import sdl2, glew  #Additional part ①
block_cipher = None
a = Analysis(['***.py'],
             pathex=['c:\\Packaged directory'],
             binaries=[],
             datas=[],
             hiddenimports=[]
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],  #Additional part ②
          name='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True )

#Additional part ③
coll = COLLECT(exe, Tree('c:\\kivy file directory'),
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               name='main')

In my case, I created a folder (folder such as Layout) for ***. Kv in the same directory as the python file and put it in it. (It may not make much sense, but don't pick up unnecessary things in the collect process)

5. Completion

The main folder is created in the following directory, and a new exe file is created in it. All the files of the dependency exist in this, and when distributing the application, each main folder You will need it. (I think it can be more unified. This is the limit for me now.) If you can hide it in a directory somewhere and use only the shortcut of the exe file well I compromised because I thought it would be like that.

dist
|_main    #← New exe file in this
|_***.exe

In particular, I used matplotlib == 3.1.1, but I found some information that could not be done with the latest version.

For the time being, GUI applications can now be distributed.

Recommended Posts

GUI application by Kivy (including matplotlib)
Multiple file processing with Kivy + Matplotlib + Draw Graph on GUI
pandas Matplotlib Summary by usage
Histogram transparent overlay by Matplotlib
Python application: data visualization # 2: matplotlib
Real-time graph display by matplotlib