2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-27 20:24:09 +00:00
|
|
|
import pandas as pd
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
# Yet another example of showing CSV data in Table
|
2018-09-15 19:44:57 +00:00
|
|
|
|
|
|
|
def table_example():
|
2019-10-23 20:10:03 +00:00
|
|
|
|
|
|
|
sg.set_options(auto_size_buttons=True)
|
|
|
|
filename = sg.popup_get_file(
|
|
|
|
'filename to open', no_window=True, file_types=(("CSV Files", "*.csv"),))
|
2018-09-15 19:44:57 +00:00
|
|
|
# --- populate table with file contents --- #
|
2018-09-20 01:11:45 +00:00
|
|
|
if filename == '':
|
2019-10-23 20:10:03 +00:00
|
|
|
return
|
|
|
|
|
2018-09-15 19:44:57 +00:00
|
|
|
data = []
|
|
|
|
header_list = []
|
2019-10-23 20:10:03 +00:00
|
|
|
button = sg.popup_yes_no('Does this file have column names already?')
|
|
|
|
|
2018-09-15 19:44:57 +00:00
|
|
|
if filename is not None:
|
|
|
|
try:
|
2019-10-23 20:10:03 +00:00
|
|
|
# Header=None means you directly pass the columns names to the dataframe
|
|
|
|
df = pd.read_csv(filename, sep=',', engine='python', header=None)
|
2018-09-20 01:11:45 +00:00
|
|
|
data = df.values.tolist() # read everything else into a list of rows
|
|
|
|
if button == 'Yes': # Press if you named your columns in the csv
|
2019-10-23 20:10:03 +00:00
|
|
|
# Uses the first row (which should be column names) as columns names
|
|
|
|
header_list = df.iloc[0].tolist()
|
|
|
|
# Drops the first row in the table (otherwise the header names and the first row will be the same)
|
|
|
|
data = df[1:].values.tolist()
|
2018-09-20 01:11:45 +00:00
|
|
|
elif button == 'No': # Press if you didn't name the columns in the csv
|
2019-10-23 20:10:03 +00:00
|
|
|
# Creates columns names for each column ('column0', 'column1', etc)
|
|
|
|
header_list = ['column' + str(x) for x in range(len(data[0]))]
|
2018-09-15 19:44:57 +00:00
|
|
|
except:
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.popup_error('Error reading file')
|
|
|
|
return
|
2018-09-15 19:44:57 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [
|
|
|
|
[sg.Table(values=data,
|
|
|
|
headings=header_list,
|
|
|
|
display_row_numbers=True,
|
|
|
|
auto_size_columns=False,
|
|
|
|
num_rows=min(25, len(data)))]
|
|
|
|
]
|
2018-09-15 19:44:57 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Table', layout, grab_anywhere=False)
|
|
|
|
event, values = window.read()
|
|
|
|
window.close()
|
2018-09-15 19:44:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
table_example()
|