Working with Files#
Edge provides a highly scalable built-in storage space for files and folders. This is in addition to data accessed remotely with Data Connectors, and the local files managed in the Analysis App.
Files stored in Edge's internal storage can be accessed by multiple users in
your organization; they are also accessible in Jupyter notebooks via the
edge
built-in object. Note that while files are accessible to all users of
your organization, they cannot be viewed or modified by anyone outside of your
organization.
Browsing and opening files#
You can explore your files interactively using the Data App file viewer.
Files and folders can also be accessed programmatically via the edge
object automatically added to your Jupyter notebook. For example, to list
the contents of the root folder (top of the file tree), simply call
edge.files.list()
in a notebook cell.
Note
In this document, code preceded by >>>
indicates Python code that can be
included in a Python cell in a Jupyter Notebook. You run the code by
entering it into the cell without the >>>
and pressing Ctrl-Enter
on your keyboard or pressing the Run button in the toolbar.
Running this code list your files stored on Edge:
>>> edge.files.list()
['Example Folder',
'Experiments.txt',
'Imported Data',
'My Data',
'Projects',
'data.csv',
'data_cleaned.csv',
'image001.png',
'image002.png',
'image003.png']
You can use this interface to open either files or folders. Here, we open the "Imported Data" folder and assign it to a variable in the notebook. Opening a folder returns a Folder object:
>>> imported_folder = edge.files.open("Imported Data")
>>> type(imported_folder)
edge.api.Folder
You can list the contents Listing the contents of this subfolder shows the files it contains:
>>> imported_folder.list()
['datacapture001.png', 'datacapture002.png']
This lets you explore the file tree step-by-step, by opening folders and subfolders in turn.
Of course, you can open an individual file, too. What do you get in return? That depends on the file type. For example, if we open the "data.csv" file in the root folder, Edge will automatically convert it to a Pandas dataframe for us:
>>> data = edge.files.open("data.csv")
>>> type(data)
pandas.core.frame.DataFrame
Likewise, if we open an image file, we get a Python Imaging Library (PIL) image object:
>>> data = edge.files.open("image001.png")
>>> type(data)
PIL.PngImagePlugin.PngImageFile
Finally, if we just want the bytes of the file, we can get a raw "file-like" object by requesting that Edge open the file with a particular handler:
>>> data = edge.files.open("image001.png", open_with="file")
>>> type(data)
edge.api.File
>>> databytes = data.read()
>>> type(databytes)
bytes
Downloading a file#
The simplest way to download a file to your desktop is to use the
Data App. You can also download a file via the Edge API.
Every Folder
object in Edge has a download()
method. Just specify the
name of the file you want to retrieve:
>>> edge.files.download('image002.png')
If you list the contents of your Analysis App home directory, you'll see the downloaded file show up:
$ ls
MyNotebook.ipynb image002.png
Note
Running the edge.files.download()
function in a Jupyter Notebook in the
Analysis App will download the file into the home directory of your
Analysis App. If you wish to download files locally, use the Data App
or a local instance of the EdgeSession
object from the enthought_edge
Python package, available from the Enthought Deployment Manager.
Uploading Files to Edge#
The simplest way to upload a file from your desktop to Edge is by dragging and
dropping it into the Data App file browser.
You can also upload files programmatically, using edge.files
. Here's an
example. Suppose you have a file called mydata.csv
in your Analysis App
local file tree. Then, to upload it to the root folder in Edge's internal
file storage, you would open the file and pass it to edge.files.upload
:
>>> with open('mydata.csv', 'rb') as myfile:
... edge.files.upload('mydata.csv', myfile)
Other file management operations#
In the right-click menus for the Data App file browser, you'll find multiple options to move or rename files & folders, in addition to the upload/download options described above. These also have counterparts in the Edge Python API.
For example, we can make a new folder in the root by calling make_folder
:
>>> edge.files.make_folder('newfolder')
If we don't like the name, we can change it:
>>> edge.files.rename('newfolder', 'New Folder')
We can even move it into another folder:
>>> edge.files.move('New Folder', 'Imported Data/New Folder')