source: trunk/examples/zoo/loader2/loadxls.py @ 6683

Last change on this file since 6683 was 6683, checked in by dkearney, 7 years ago

adding an example of using a loader to allow users to chose an example excel spreadsheet or upload their own. the uploaded file data is stored in the driver.xml. the simulation checks whether the user chose an example excel spreadsheet or uploaded thier own. if the user uploaded their own file, we write it to disk, then parse it using a pandas dataframe and finally load it into a Rappture Curve.

File size: 2.3 KB
Line 
1import Rappture
2import sys
3import os
4import pandas as pd
5import tempfile
6
7# The name of the xls file that holds the data to plot
8xlspath = ""
9
10# Flag to tell us whether we should cleanup the file
11# whose name is stored in xlspath.
12cleanup = False
13
14# Open the XML file containing the run parameters
15rx = Rappture.PyXml(sys.argv[1])
16
17# The user chose a data file to process by picking an
18# option from the loader. Each option from the loader
19# will store a different value into the hidden string
20# "input.string(datafile)". Pull the value out of the
21# hidden string and
22datafile = rx['input.string(datafile).current'].value
23
24# Check if the user uploaded their own data file or chose
25# one of our examples from the loader. Examples from the
26# loader will be a file name starting with the prefix
27# "file://". Uploaded files will be a non-ascii blob
28# of binary.
29#
30# If the first seven bytes of the value we
31# pulled out of the driver file start with "file://",
32# then we can assume characters 7:end are the name of
33# of the xls file we can read from disk.
34#
35# Else, assume the value pulled out of the driver file
36# is a non-ascii binary blob of xls data and write it
37# to disk (in a temporary file). Store the filename in
38# our xlspath variable.
39
40if datafile[0:7] == "file://":
41    # first seven bytes match, store the file name
42    xlspath = datafile[7:]
43else :
44    # datafile hold the data from the uploaded xml file
45    # write the data from datafile to disk
46    tmp_fd,tmp_fn = tempfile.mkstemp()
47    os.write(tmp_fd,datafile)
48    os.close(tmp_fd)
49    xlspath = tmp_fn
50    cleanup = True
51
52# read the xls file into a pandas dataframe
53xlsdata = pd.ExcelFile(xlspath)
54df = xlsdata.parse('Sheet1')
55
56# create a output curve object to plot the data in the xls file
57curve = rx['output.curve(1)']
58curve['about.label'] = "xls file"
59curve['about.description'] = "xls file data plotted as an xy curve"
60curve['xaxis.label'] = "X axis"
61curve['xaxis.description'] = "X axis from the provided data file"
62curve['yaxis.label'] = "Y axis"
63curve['yaxis.description'] = "Y axis from the provided data file"
64# save the 'x' and 'y' columns from the xls data in a Rappture Curve
65curve['component.xy'] = (df['x'], df['y'])
66
67# cleanup the temp file from user uploaded data
68if cleanup is True:
69    os.remove(xlspath)
70
71# save the updated XML describing the run...
72rx.close()
Note: See TracBrowser for help on using the repository browser.