Where To Put Large Python Lists
Solution 1:
I personally think having the data in a separate file is the cleanest way.
You could for example just create a csv
or json
or simple text file from another application and just place it in the right directory.
It seems, that pysimpleGUI does not create executables by itself, it seems to be using pyinstaller
( https://pysimplegui.readthedocs.io/en/latest/#creating-a-windows-exe-file )
Pyinstaller allows to add non python files (like for example in your case json or csv files) to its executable.
When the executable is started a temporary directory is created and all the python and the added files will be extracted and stored there.
You have to use the switch
--add-data *source_path*:**destination_path**
Below example shows how to do this.
just create in the directory where your python script is installed a directory named files
and place your datafiles you want to be bundled with your application inside it.
For this example this will be one file named files/info.csv
For this example to work it should contain at least one line.
Then create your python file (for this test with following contents)
example filename: sg_w_fread.py
import os
import sys
import PySimpleGUI as sg
if getattr(sys, "frozen", False):
# for executable mode
BASEDIR = sys._MEIPASS
else:
# for development mode
BASEDIR = os.path.dirname(os.path.realpath(__file__))
print("BASEDIR =", BASEDIR)
fname = os.path.join(BASEDIR, "files", "info.csv")
# just for debugging
print("FNAME", repr(fname))
print(os.path.isfile(fname))
sg.theme('DarkAmber') # Add a touch of color
# read something from a file
with open(fname) as fin:
txt = fin.read().split("\n")[0]
layout = [ [sg.Text("first line of file is '" + txt + "'")], # display something from the file
[sg.Text('Enter something on Row 2'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Cancel')] ]
window = sg.Window('Window Title', layout)
while True:
event, values = window.read()
if event in (None, 'Cancel'): # if user closes window or clicks cancel
break
print('You entered ', values[0])
window.close()
and type following command to create the executable:
pyinstaller -wF sg_w_fread.py --add-data files:files
Post a Comment for "Where To Put Large Python Lists"