How-to: File input / output
ArupCompute has no direct facility for taking files as input. However, in combination with the clients this can be achieved:
- Client (e.g. Excel) reads the contents of the file
- Client send contents of file over network to your library
- Your library accepts a text input (representing the contents of the file)
- Your library processes the file
Naturally the same process will work in reverse for sending a file back to the user.
There are practical limitations on the size of file that can be transferred this way. Anything over a megabyte may fail (especially when executed in batch mode).
If you need to move large files around it will be better to get users to upload the files somewhere accessible, and then determine a method for your code to access the shared location. ArupCompute functions only have access to the open internet, so authentication to any secured areas would need to be addressed.
Intermediate local file
Some libraries expect to take a filepath as input (rather than file contents).
To workaround this the snippet below creates a temporary file to work with.
This snippet works for local testing, as well as in the cloud.
def writetemp(x: str) -> str:
# find the 'temporary' directory on this system
dir = gettempdir()
# decide on a name for your temporary file, in this case 'myfile.txt'
# NOTE: use os.path.join as in the cloud your code runs on Linux rather than Windows
# NOTE: may want to generate a unique filename each time in case multiple users
# are accessing your library at any one time
path = os.path.join(dir,'myfile.txt')
# create and open the temporary file, write the user input to it
# (potentially the contents of a file read as per the procedure above)
with open(path,'w') as f:
f.write(x)
# below just shows reading that temporary file
# and returning it to the user to prove it has worked
# replace with your application logic
y = ''
with open(path,'r') as f:
y = f.read()
return y
ArupCompute functions run in the cloud and there is no guarantee that files written will exist after the function has finished executing.
No data can be persisted on the temporary areas. If data needs to be saved it must be exported to somewhere else (e.g. to the user, or to a shared area).