Select to view content in your preferred language

File Output Paramter in WebTool that was published from a Notebook in ArcGIS Online

2759
12
Jump to solution
07-22-2024 08:51 AM
Sven_Harpering
Esri Contributor

Hello everyone,

we have created a notebook in ArcGIS Online that creates a Word document based on various parameters. If we let the notebook run on its own, everything works. The output is written to the "arcgis/home" directory.

The next step would be to publish the notebook as a web tool in ArcGIS Online. To do this, we wanted to define an output parameter. The settings currently look like this:

Sven_Harpering_0-1721663506679.png

 

A first question would be what we have to specify as a default value in JSON format. According to the documentation, it has to be a URL to an existing output? Currently, we have simply uploaded the Word template as an item in ArcGIS Online and specified the URL to it.

Afterwards we added this snippet at the end of the notebook:

Sven_Harpering_1-1721663506683.png

 

The name of the variable equals the output variable name for the word file.

After publishing, we open the tool in map viewer and want to run it. Here we immediately notice that the output parameter does not appear in the GUI.

Sven_Harpering_2-1721663507188.png

 

The tool can still be started, but aborts with an error message in the output parameter area. 

Sven_Harpering_3-1721663506701.png

 

It seems that the error is in the output parameter section. Do you have any clue, what we are doing wrong in the whole output parameter section (Notebook config, etc.)? The input parameters work.

Best

Sven

 

Passionate about GIS and on the journey as an instructor for analytical insights.
12 Replies
Alex_Rodriguez
Emerging Contributor

josh_sample.output.jpgFinally figured it out.   Still have a problem though...
I have the tool running now (added the output parameter: that's what I was missing before.)

So the tool runs and creates an output "STRING".   So the new problem is, what am I supposed to do with the output string that is displayed in the "details" page after the tool completes its run?  I'm not able to copy the text from that details window. 
Anyone have ideas for that?

 

0 Kudos
xlt208
by Esri Contributor
Esri Contributor

Hi @Alex_Rodriguez,

Thanks for your patience! I would like to suggest the following edits to your code snippet that creates a CSV file when the notebook is run as a web tool:

 

f = open("GroupsList.csv", mode='a', encoding='utf-8')
f.write("whatever I'm writing to the file, line by line")
f.close()
output_csv = {"url": "GroupsList.csv"}

 

Here is how the output parameter should be configured.

xlt208_0-1732219433029.png

The second cell was inserted using the "insert into notebook" button. During a web tool job run, this cell sends the value of the notebook variable to the web tool job as an output parameter.

xlt208_0-1732219605340.png

With the configurations above, the web tool should output a CSV file that you can click to download.

xlt208_1-1732220267706.png

Please note that files created during notebook web tool job runs are not stored or retained in the workspace directory (/arcgis/home). If you wish to keep the files, you can add them to the content as items. If there are other members who will be running the tool, the items will be added to their content. In that case, you may consider sharing the items with a group so that all items are organized in the same place. Here is a code snippet demonstrating the workflow for adding and sharing items:

from datetime import datetime as dt
from arcgis.gis import GIS

# creating csv file
csv_name = f"test_{dt.now().strftime('%Y%m%d%H%M%S')}"
csv_path = f"{csv_name}.csv"
f = open(csv_path, mode="a", encoding="utf-8")
f.write("test1, test2, test3, test4")
f.close()
output_csv = {"url": csv_path}

# adding csv file as item
gis = GIS("home")
folder_name = "CSVs"
folder = gis.content.folders.get(folder_name) or gis.content.folders.create(folder_name)
csv_item = folder.add(
    item_properties={"title": csv_name, "type": "CSV"},
    file=csv_path
).result()

# sharing csv item to group
group_name = "CSVs Generated from Notebook Web Tools"
group = gis.groups.search(group_name)[0] if gis.groups.search(group_name) else gis.groups.create(
    title=group_name,
    tags=["csv"],
    membership_access="org"
)
csv_item.sharing.groups.add(group)

 

Please let me know if you have any questions!

Best,

Lingtao

Sven_Harpering
Esri Contributor

@xlt208, thank you very much! 

Passionate about GIS and on the journey as an instructor for analytical insights.