0

I have a python script in which I have numerous PyQt5 widgets. The script basically calculates some data which is stored mostly in lists. This data is then used with PyQt5 widgets and matplotlib tools to present the results.

What I want to do is a very common question which I am aware is asked many times. However, I could not come up with a post which involves PyQt5 and matplotlib. So I want to store all the data that is calculated and all the PyQt5 widgets and matplotlib plots to load it later.

The most practical method I saw in my search is pickle. However, with pickle I have to 'dump' the results as a list containing floats and then remember the order in which they were dumped to reassign them when I load the pickled file. This is a tedious process and also there is the problem of losing float precision using pickle.

My question is:

  1. Is there any alternative method you would recommend other than pickle to save a workspace which includes data consisting of floats, lists of floats, PyQt5 widgets and matplotlib plots and tables? I saw QSettings but as far as I understood it deals with only appearance of widgets.

  2. If pickle is the most practical way, is there a way I can create a dictionary with the name of the variable as the key and its value as the value of the dictionary, so I do not have to reassign pickle-loaded file's indexes back to my variables one by one?

A very basic script to help explain what I want to do:

def input():

x = []
y = []
for i in range(0,10):
    x.append(i*2)
    y.append(i*4)
func(x, y)

def func(x, y):

'''
matplotlib plot initiation code 
FigureCanvas initiation code
'''

plot.axes.plot(x, y)
plotcanvas.draw()

I want to save x, y, plot and plotcanvas to be able to load it later in a practical way.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Try this https://stackoverflow.com/a/15463472/6701627 or this https://stackoverflow.com/a/20725705/6701627. It might help you. You need to save all your objects in one file and retrieve them, they will be returned probably as a list. – thepunitsingh Dec 22 '20 at 10:32
  • Just to be clear, you cannot "save widgets". Libraries like pickles allows saving data by converting them to bytes, and this is generally possible with basic data models provided by the standard library. This is not possible with Qt widgets, and you need to provide your own (un)serialization method to do so. – musicamante Dec 22 '20 at 10:41
  • Thank you thepunitsingh and musicamante. I I dealt with help of your comments. – atakulaksizoglu Dec 24 '20 at 09:25

0 Answers0