Version 1 (modified by dkearney, 18 years ago) (diff) |
---|
Uploading with Rappture
Topics discussed:
- Uploading Basics
- Uploading Data Example
Uploading Basics
Rappture developers can allow application users to upload data into the application by using Rappture's Upload capabilities. This is usesful if you have an older application that requires a complicated input deck or if the input deck is actually the output of another application. You can allow the user to upload data into the Rappture GUI using the "Upload" capabilities of the loader. To learn more about how loaders work, check out the wiki page on loaders and pay particular attention to the "Upload / Download" section.
Uploading Data Example
This example demonstrates using the upload capabilities of Rappture. The following files are used in this example:
- sineIt.py - python script representing some science code
- inputDeck - cryptic input deck used by our science code
- wrapper.py - python script representing a wrapper script
- tool.xml - Rappture tool description file
Here is what each file should look like:
sineIt.py
#! /bin/sh # """:" exec python $0 ${1+"$@"} """ # This script represents a bit of science code that takes in 3 numeric # values and outputs a file with values of a sine function. import sys from math import sin,pi if __name__ == '__main__': inDeckName = sys.argv[1] # try to open our file try: fh = open(inDeckName) except IOError: # we'll pass on dealing with the exception for now # we'll just exit with a message print 'Error opening file %s' % inDeckName sys.exit(-1) # read the special cryptic inputDeck # inputDeck contains 3 numbers, each on a separate line # the first number is the start value. # the second number is the end value. # the third number is the step. # here we are splitting on whitespace (in this case, newlines '\n') [start,end,step] = fh.read().split() fh.close() x = float(start) outData = '' while x < float(eval(end)): # perform our sin calculation y = sin(x) # save the values to a string outData += '%g %g\n' % (x,y) # increment our loop control variable x = x + float(step) # write our saved calculation values to disk fh2 = open(inDeckName+'.out','w') fh2.write(outData) fh2.close() # exit successfully sys.exit(0)
inputDeck
0 4*pi 0.1
wrapper.py
#! /bin/sh # """:" exec python $0 ${1+"$@"} """ # This script represents a bit of science code that takes in 3 numeric· # values and outputs a file with values of a sine function. import sys import os import re import Rappture def getCommandOutput(command): childin,childout,childerr = os.popen3(command) data = childout.read() err = childerr.read() errcode = childout.close() if err: print '%s\n%s' % (command, err) sys.exit(-1) return data def getDriverId(driverFileName): idObj = re.search('\d+',driverFileName) if idObj != None: return idObj.group() return None if __name__ == '__main__': inDeckName = sys.argv[1] lib = Rappture.library(inDeckName) if lib == None: # we'll pass on dealing with the exception for now # we'll just exit with a message print 'Error opening file %s' % inDeckName sys.exit(-1) inputType = lib.get('input.loader.current') if inputType == 'Uploaded data': writeDataLocation = lib.get('input.loader.upload.to') writeData = lib.get(writeDataLocation+'.current') else: start = lib.get('input.string(start).current') end = lib.get('input.string(end).current') step = lib.get('input.number(step).current') writeData = '%s\n%s\n%s' % (start,end,step) outFileName = 'inputDeck.' + getDriverId(inDeckName) fh = open(outFileName, 'w') if fh != None: fh.write(writeData) fh.close() # run the science code getCommandOutput('./sineIt.py %s' % outFileName) dataFileName = outFileName + '.out' # try to open our data file try: dFH = open(dataFileName) except IOError: # we'll pass on dealing with the exception for now # we'll just exit with a message print 'Error opening file %s' % dataFileName sys.exit(-1) data = dFH.read() dFH.close() lib.put('output.curve(sin_data).about.label','sin curve') lib.put('output.curve(sin_data).about.label','sample output of the sin function') lib.put('output.curve(sin_data).component.xy',data) Rappture.result(lib) sys.exit(0)
tool.xml
<?xml version="1.0"?> <run> <tool> <title>upload example</title> <about>Example of Uploading with Rappture GUI.</about> <command>@tool/wrapper.py @driver</command> </tool> <input> <loader> <about> <label>Example</label> <description>Use this to load examples.</description> </about> <upload> <to>input.string(uploadedFile)</to> <prompt>Upload Input Deck.</prompt> </upload> </loader> <string id="uploadedFile"> <about> <label>Uploaded File</label> </about> <size>5x5</size> <default></default> </string> <string id="start"> <about> <label>start</label> </about> <default>-1</default> </string> <string id="end"> <about> <label>end</label> </about> <default>-1</default> </string> <number id="step"> <about> <label>step</label> </about> <default>-1</default> </number> </input> </run>
Attachments (4)
-
inputDeck
(11 bytes) -
added by dkearney 18 years ago.
example input deck for science code
-
sineIt.py
(1.3 KB) -
added by dkearney 18 years ago.
sample science code
-
tool.xml
(926 bytes) -
added by dkearney 18 years ago.
example tool.xml
- wrapper.py (3.1 KB) - added by dkearney 16 years ago.
Download all attachments as: .zip