'HDF5 store added to application does not keep data after closing application
I have an application, which uses a HDF5
-file to store dataframes
. I included the file with PyInstaller
into the application. When I run the application I can save data to the store. I can access the data as long as the program is running. But after closing the application all data, which I have stored in the HDF5
-file is gone.
A minimal example, which produces this, is:
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
import pandas as pd
import sys, os
import h5py
class MyApp(QMainWindow):
def __init__(self):
super().__init__()
self.window_width, self.window_height = 200, 200
self.setMinimumSize(self.window_width, self.window_height)
self.button = QPushButton("Save", self)
self.button.move(0, 50)
self.button.clicked.connect(self.save)
self.button = QPushButton("Load", self)
self.button.move(0, 100)
self.button.clicked.connect(self.load)
self.button = QPushButton("Delete", self)
self.button.move(0, 150)
self.button.clicked.connect(self.delete)
self.df = pd.DataFrame(data = [1, 2, 3])
def load(self):
h5File = self.resource_path("test.h5")
self.df = pd.read_hdf(h5File, "data")
print(self.df)
def save(self):
h5File = self.resource_path("test.h5")
frame = [self.df, self.df]
self.df = pd.concat(frame)
self.df.to_hdf(h5File, "data")
def delete(self):
h5File = self.resource_path("test.h5")
with h5py.File(h5File, 'r+') as store:
del store["data"]
self.df = pd.DataFrame(data = [1, 2, 3])
def resource_path(self, relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = MyApp()
myapp.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
I can save df
in the store and then load it too. When I close the program, however, the memory is empty again.
The spec
-file, which I use with PyInstaller
is:
block_cipher = None
a = Analysis(['test.py'],
pathex=['C:\\Users\\mazze\\PycharmProjects\\LawRefrences\\test8.py'],
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)
a.datas += [("test.h5","C:\\Users\\mazze\\PycharmProjects\\LawRefrencer\\test.h5", "DATA")]
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='test8',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|