-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabrowser.py
96 lines (76 loc) · 3.1 KB
/
databrowser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pickle
import pandas as pd
import glob
import os.path
from IPython.display import Image, HTML
def listData():
"""Returns a table of all the saved data files in a table with description,
date, and thumbnail. This is HTML rendered in order to display .png
pictures"""
#Prevent truncation of long strings. Also necessary for images which have
#long file names, especially including HTML formatting
pd.set_option('display.max_colwidth', -1)
data_df = _dataTable()
#HTML function renders the literal text in each column as HTML
return HTML(data_df.to_html(escape=False))
def _load(filename):
"""Input is the pickled file that was automatically created from measurement
or use of save function. Returns savedData object"""
with open(filename, 'rb') as file:
savedData = pickle.load(file)
return savedData
def loadnum(number):
"""Load filename by index of given by listData. Returns savedData object.
Args:
number: index of file to be loaded. Use listData to see indices.
"""
datafiles = glob.glob('*.p')
data_table = {'Date': [], 'Description': [], 'Comment': []}
i = 0
for file in datafiles:
try:
data = _load(file)
if i == number:
return data
i += 1
except EOFError:
pass
return 'Index out of range!'
def query(comment):
"""Returns table of all data that contains the input comment
Args:
comment: Comment to search for. Looks for comments that just contain
input comment ie 'o' will return data with the comment of 'good'
"""
pd.set_option('display.max_colwidth', -1)
data_df = _dataTable()
data_df_queried = data_df.loc[data_df.loc[:,'Comment'].str.contains(comment)]
return HTML(data_df_queried.to_html(escape=False))
def _dataTable():
"""Returns Pandas Dataframe that is used to display table in listData or
query
"""
#Get all files with .p extension
datafiles = glob.glob('*.p')
data_table = {'Date': [], 'Description': [], 'Comment': [], 'Thumbnail': []}
for file in datafiles:
try:
data = _load(file)
data_table['Date'].append(data.date)
data_table['Description'].append(data.description)
if data.comment:
data_table['Comment'].append(data.comment)
else:
data_table['Comment'].append('')
thumbnail_file = './DataThumbnails/' +file[:-1] +'png'
if os.path.isfile(thumbnail_file):
#If a picture exists, add as thumbnail using HTML formatting
data_table['Thumbnail'].append('<img src="%s" height="50" width="50"/>' % (thumbnail_file,))
else:
data_table['Thumbnail'].append('')
except EOFError:
pass
#Create Pandas DataFrame based off dictionary, then rearrange columns
data_df = pd.DataFrame(data = data_table)
data_df = data_df[['Date', 'Description', 'Comment', 'Thumbnail']]
return data_df