attempting to upload files to Notebook Files using arcgis.gis.nb

956
7
Jump to solution
05-11-2023 09:23 AM
Labels (1)
Levon_H
New Contributor III

Hi folks.  We're attempting to move files from our local network to our AGOL Notebook "Files" location programmatically, using ArcGIS API for Python. It looks like the arcgis.gis.nb module may be the solution, using NotebookDataAccess.  

API reference here:

https://developers.arcgis.com/python/api-reference/arcgis.gis.nb.html#notebookdataaccess

In the end, I've got the Boolean to be "True" as in, it worked.  However, when I go to my Notebook Files, nothing is there at "/arcgis/home".

 

Here's what I have so far (working in Jupyter Notebooks):

import arcgis.gis.nb
import arcgis.gis.nb._dataaccess
from arcgis.gis.nb._dataaccess import NotebookDataAccess
import arcpy
from arcgis.gis import GIS
from arcgis.gis.nb import NotebookManager, Notebook, NotebookServer, _dataaccess
gis = GIS('Home')

# URL to the desired Notebook
url = "https://levon.maps.arcgis.com/home/notebook/notebook.html?id=d693b3892c8747a892adxxxxxxxcb#"

# path to the zipfile on our network
zipfgdb = r"N:\gis\Levon\Sandbox\LIVE_TEST.zip"

# test to see if I'm actually pinging AGOL Notebook
nb_server = gis.notebook_server[0]
nb_server

# test to see if I'm accessing the correct notebook
notebook_item = gis.content.get('d693b3892c8747a892xxxxxxxxx30cb')
notebook_item

# attempt to upload the zipfile from our network to Notebook /arcgis/home Files
st_up = NotebookDataAccess(url, gis)
st_up.upload(zipfgdb)

Result: "True"

So, it's saying True, as in it worked, but when I check, nothing is there.  See below.  Any help!?  Be nice, I'm a intermediate/beginner! 🙂  We've been working on this for days, this is the closest we've gotten.

Levon_H_0-1683821759046.png

 

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
JohnYaist1
Esri Contributor

Hi Levon_H -

I misstyped the code in my first code snippet. I'm sorry for my mistake - the correct snippet is below:

from arcgis.gis import GIS
from arcgis.gis.nb._dataaccess import NotebookDataAccess

ago = GIS(profile="your_online_profile")
connected_user = gis.users.me

file_path="local/path/to/data/shpfile.zip"

nb_da_url = connected_user.generate_direct_access_url(store_type="notebook")
nb_daccess = NotebookDataAccess(url=nb_da_url["url"], gis=ago)

nb_daccess.upload(file_path)

The generate_direct_access_url() method on a User object returns a Python dictionary with a key named url whose values is the organizational endpoint necessary to initialize a NotebookDataAccess object for  uploading files to the ArcGIS Online files area. As you noted, the url is not to the notebook itself, but to a system administration endpoint for the uploading operation. Glad this worked - and sorry for the confusion. We'll work to adjust the documentation. 

View solution in original post

7 Replies
JohnYaist1
Esri Contributor

 

 

Hi Levon_H

The Python API reference documentation currently does not provide an example of how to accomplish your workflow. Sorry for the confusion caused, and we'll update that documentation to provide an example.

There is a method on the User class called generate_direct_access_url() that serves to provide the proper url argument to initialize a NotebookDataAccess object for loading files into the /arcgis/home directory.  Here is a snippet to accomplish this. The following will create a subdirectory in home called notebookworkspace:

 

 

from arcgis.gis import GIS
from arcgis.gis.nb._dataaccess import NotebookDataAccess

ago = GIS(profile="your_online_profile")
connected_user = gis.users.me

file_path="local/path/to/data/shpfile.zip"

nb_da_url = connected_user.generate_direct_access_url(store_type="notebook")
nb_daccess = NotebookDataAccess(url=nb_da_url, gis=ago)

nb_daccess.upload(file_path)

 

 Hope this helps.

 

 

 

Levon_H
New Contributor III

Hi John,

We're still working on this, and I'm just now seeing your response.  So thank you and apologies.  

 

I've been trying this, and I still can't get it to work.  I was told this:

To fix this you have to modify this line:
nb_daccess = NotebookDataAccess(url=nb_da_url, gis=ago)

to this: 
nb_daccess = NotebookDataAccess(url=nb_da_url[“url”], gis=ago)

It worked, thanks!!   

I'm still confused as to what the ["url"] is.  I thought it was the url of my notebook, so I put that in at first, of course that didn't work.  When I changed it back to just ["url'], that worked.

 

0 Kudos
JohnYaist1
Esri Contributor

Hi Levon_H -

I misstyped the code in my first code snippet. I'm sorry for my mistake - the correct snippet is below:

from arcgis.gis import GIS
from arcgis.gis.nb._dataaccess import NotebookDataAccess

ago = GIS(profile="your_online_profile")
connected_user = gis.users.me

file_path="local/path/to/data/shpfile.zip"

nb_da_url = connected_user.generate_direct_access_url(store_type="notebook")
nb_daccess = NotebookDataAccess(url=nb_da_url["url"], gis=ago)

nb_daccess.upload(file_path)

The generate_direct_access_url() method on a User object returns a Python dictionary with a key named url whose values is the organizational endpoint necessary to initialize a NotebookDataAccess object for  uploading files to the ArcGIS Online files area. As you noted, the url is not to the notebook itself, but to a system administration endpoint for the uploading operation. Glad this worked - and sorry for the confusion. We'll work to adjust the documentation. 

Levon_H
New Contributor III

Excellent.  Thank you.  👍

0 Kudos
Tim_E
by
New Contributor

I am trying to use above code to upload files to our ArcGIS Enterprise Notebook files folder, but generate_direct_access_url() does not return anything. Am I missing something?

0 Kudos
Levon_H
New Contributor III

Hi Tim,

Not sure if you'd be expecting anything to return.  Maybe John can jump in here.  From my understanding, that simply allows access to your online Notebook files.  Those 12 lines of code above are all I'm using.  In that example, you'd find shpfile.zip in your Notebook files.  Hope that helps.

0 Kudos
Tim_E
by
New Contributor

Hi Levon,

I tried to replicate it, but got an error saying:

TimElrick_0-1712248180268.png

Hence, I assumed the empty return of generate_direct_access_url() might have to do with it. If I understand correctly, the difference between your use case and mine is that you use Arc Online while I try to use Arc Enterprise.