1 | import Rappture |
---|
2 | import sys |
---|
3 | import os |
---|
4 | import pandas as pd |
---|
5 | import tempfile |
---|
6 | |
---|
7 | # The name of the xls file that holds the data to plot |
---|
8 | xlspath = "" |
---|
9 | |
---|
10 | # Flag to tell us whether we should cleanup the file |
---|
11 | # whose name is stored in xlspath. |
---|
12 | cleanup = False |
---|
13 | |
---|
14 | # Open the XML file containing the run parameters |
---|
15 | rx = 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 |
---|
22 | datafile = 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 | |
---|
40 | if datafile[0:7] == "file://": |
---|
41 | # first seven bytes match, store the file name |
---|
42 | xlspath = datafile[7:] |
---|
43 | else : |
---|
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 |
---|
53 | xlsdata = pd.ExcelFile(xlspath) |
---|
54 | df = xlsdata.parse('Sheet1') |
---|
55 | |
---|
56 | # create a output curve object to plot the data in the xls file |
---|
57 | curve = rx['output.curve(1)'] |
---|
58 | curve['about.label'] = "xls file" |
---|
59 | curve['about.description'] = "xls file data plotted as an xy curve" |
---|
60 | curve['xaxis.label'] = "X axis" |
---|
61 | curve['xaxis.description'] = "X axis from the provided data file" |
---|
62 | curve['yaxis.label'] = "Y axis" |
---|
63 | curve['yaxis.description'] = "Y axis from the provided data file" |
---|
64 | # save the 'x' and 'y' columns from the xls data in a Rappture Curve |
---|
65 | curve['component.xy'] = (df['x'], df['y']) |
---|
66 | |
---|
67 | # cleanup the temp file from user uploaded data |
---|
68 | if cleanup is True: |
---|
69 | os.remove(xlspath) |
---|
70 | |
---|
71 | # save the updated XML describing the run... |
---|
72 | rx.close() |
---|