Generate a file
You can use files as input or output parameters
using file
and blob
annotations.
The file
parameter type allows you to read and write a file.
Inside the Python
/R
script this parameter will be a string variable
containing a path to the local file.
For example, let's save a dataframe to a JSON file:
- Result
- Python

#name: DfToJSON
#description: Saves a dataframe to JSON file
#language: python
#tags: template, demo, FileIo
#input: dataframe df [Dataframe to convert to JSON]
#output: file json_file
df.to_json(json_file)
When you run this script, Datagrok will return the FileInfo
object in the scalar variables panel.
To save the file, right-click on the highlighted file link and choose the Download option.
The file name always matches the output variable name.
Some Python functions (for example, Numpy Save) automatically add an extension to the file name if it is provided without an extension. In this case, Datagrok won't be able to locate output file, and you'll see empty file in the Datagrok output.
To override this behavior, you can open the file via the Python open
function,
and use the file object instead of the file name.
#output: file array_file_binary
...
with open(array_file_binary, 'wb') as npfile:
np.save(npfile, array)
...