diff --git a/samples/05_content_publishers/publishing_sd_shapefiles_and_csv.ipynb b/samples/05_content_publishers/publishing_sd_shapefiles_and_csv.ipynb index 876c50d2df..1a2ae7b864 100644 --- a/samples/05_content_publishers/publishing_sd_shapefiles_and_csv.ipynb +++ b/samples/05_content_publishers/publishing_sd_shapefiles_and_csv.ipynb @@ -23,15 +23,37 @@ "
" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Import Libraries" + ] + }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from IPython.display import display\n", "from arcgis.gis import GIS\n", - "import os\n", + "import os" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Connect to GIS" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "gis = GIS('Home')" ] }, @@ -39,7 +61,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In order to have the codes below run smoothly, use the pre-requisite cells as in cell[2] to delete existing .zip, .sd, or services from the `gis` content, and in cell [3] to delete existing folder." + "## Remove existing content\n", + "\n", + "For this illustration, in order to have the code and data operations below run smoothly, we'll use the cells below to delete existing .zip, .sd, or services from the `gis` content and delete the existing folder that could cause conflict during publishing." ] }, { @@ -65,7 +89,7 @@ " search_result = gis.content.search(query=file_name, item_type=current_item_type)\n", " if len(search_result) > 0:\n", " for item in search_result:\n", - " item.delete()\n", + " item.delete(permanent=True) # if recycle bin is enabled, otherwise the parameter is ignored\n", " print(\"Deleted existing \" + current_item_type + \": \", item)\n", " \n", "item_types = [\"Service Definition\", \"Feature Layer Collection\", \"Map Service\"]\n", @@ -100,7 +124,8 @@ "source": [ "def delete_existing_folder(folder_name):\n", " try:\n", - " return gis.content.delete_folder(folder=folder_name)\n", + " folder = gis.content.folders.get(folder_name)\n", + " return folder.delete(permanent=True) # if recycle bin is enabled, otherwise the parameter is ignored\n", " except:\n", " return False\n", "\n", @@ -113,7 +138,7 @@ "metadata": {}, "source": [ "\n", - "# Publish all the service definition files in a folder\n", + "## Publish all the service definition files in a folder\n", "\n", "The sample below lists all the service definition (.sd) files in a data directory and publishes them as web layers. To publish a service definition file, we first add the .sd file to the Portal, and then call the publish() method:" ] @@ -204,11 +229,17 @@ } ], "source": [ + "root_folder = gis.content.folders.get()\n", "# Loop through each file and publish it as a service\n", "for current_sd_file in sd_file_list:\n", - " item = gis.content.add({}, data_dir + current_sd_file) # .sd file is uploaded and a .sd file item is created\n", + " item = root_folder.add(\n", + " item_properties={\n", + " \"title\": \"New Layer\", \n", + " \"type\": \"Service Definition\"\n", + " }, \n", + " file=data_dir + current_sd_file).result() # .sd file is uploaded and a .sd file item is created\n", " if \"BuildCache\" not in current_sd_file:\n", - " published_item = item.publish() # .sd file item is published and a web layer item is created\n", + " published_item = item.publish() # .sd file item is published and a web layer item is created\n", " else:\n", " published_item = item.publish(build_initial_cache=True) # publish as hosted tile layer with \"build cache\" enabled\n", " display(published_item)" @@ -225,7 +256,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> **Note**: When publishing an SD file to Enterprise with customized `publish_parameters`, as specified in [REST API](https://developers.arcgis.com/rest/users-groups-and-items/publish-item.htm), the customization of item properties, such as title, snippets, tags, and description, will not be reflected in the properties of online SD file, or the published service. Instead, the SD file item, and the service item will pertain the local SD file's original settings. A recommended workflow is to use `update()` method after uploading the SD file, and publishing the service." + "> **Note**: When publishing an SD file to Enterprise with customized `publish_parameters`, as specified in [REST API](https://developers.arcgis.com/rest/users-groups-and-items/publish-item.htm), the customization of item properties, such as title, snippets, tags, and description, will not be reflected in the properties of online SD file, or the published service. Instead, the SD file item, and the service item will contain the local SD file's original settings. A recommended workflow is to use `update()` method after uploading the SD file, and publishing the service." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we'll connect to an ArcGIS Enterprise deployment and publish a service definition file and illustrate the updating of item properties published from them:" ] }, { @@ -239,26 +277,23 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ - "sd_file = \"data/publishing_sd_shapefiles_and_csv/Nursing_home_locations.sd\"\n", - "\n", - "item_properties = {\n", - " \"title\": \"Nursing Home Locations from SD file\",\n", - " \"tags\" : \"test 1st, test 2nd, test 2023\",\n", - " \"snippet\": \"test snippet 123456\",\n", - " \"description\": \"test description\",\n", - " \"type\": \"Feature Collection\",\n", - " \"typeKeywords\": \"Data, Feature Collection, Singlelayer\", \n", - " 'access': 'org'\n", - "}" + "sd_file = \"data/publishing_sd_shapefiles_and_csv/Nursing_home_locations.sd\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's add the service definition file item:" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -267,28 +302,63 @@ "['opendata', 'health', 'automation', 'ArcGIS', 'Service Definition', '.sd']" ] }, - "execution_count": 3, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "uploaded_file = p_gis.content.add({}, sd_file)\n", + "proot_folder = p_gis.content.folders.get()\n", + "uploaded_file = proot_folder.add({\n", + " \"title\": \"Nursing Home Location\", \n", + " \"type\": \"Service Definition\"}, file=sd_file).result()\n", "uploaded_file.tags" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We'll update some of the item properties for the service definition file by defining a dictionary and inputting it the [Item.update()](/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update) method." + ] + }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "item_properties = {\n", + " \"title\": \"Nursing Home Locations from SD file\",\n", + " \"tags\" : uploaded_file.tags + [\"test 1st\", \"test 2nd\", \"test 2023\"],\n", + " \"snippet\": \"Nursing homes in Washington state added in API\",\n", + " \"description\": \"Locations of nursing homes in US state of Washington\",\n", + " \"type\": \"Feature Service\",\n", + " \"typeKeywords\": \"Data, Feature Service, Hosted Service, Service, Singlelayer\", \n", + " 'access': 'org'\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['test 1st', 'test 2nd', 'test 2023']" + "['opendata',\n", + " 'health',\n", + " 'automation',\n", + " 'ArcGIS',\n", + " 'Service Definition',\n", + " '.sd',\n", + " 'test 1st',\n", + " 'test 2nd',\n", + " 'test 2023']" ] }, - "execution_count": 4, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -300,7 +370,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -309,28 +379,37 @@ "['opendata', 'health', 'automation', 'ArcGIS', 'Service Definition', '.sd']" ] }, - "execution_count": 5, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "# Publish the service from the service definition item\n", "item = uploaded_file.publish()\n", "item.tags" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['test 1st', 'test 2nd', 'test 2023']" + "['opendata',\n", + " 'health',\n", + " 'automation',\n", + " 'ArcGIS',\n", + " 'Service Definition',\n", + " '.sd',\n", + " 'test 1st',\n", + " 'test 2nd',\n", + " 'test 2023']" ] }, - "execution_count": 6, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -349,30 +428,38 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 34, "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Recycle bin not enabled on this organization. Permanent delete parameter ignored.\n", + "Recycle bin not enabled on this organization. Permanent delete parameter ignored.\n" + ] + }, { "data": { "text/plain": [ "True" ] }, - "execution_count": 7, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "item.delete()\n", - "uploaded_file.delete()" + "item.delete(permanent=True)\n", + "uploaded_file.delete(permanent=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# Publish a feature service from a shapefile and update the item information\n", + "## Publish a feature service from a shapefile and update the item information\n", "\n", "To publish a shapefile, we first add the zipped shapefile to the Portal as an item, then call publish() method on the item to create a web layer. Often times, your shape files or service definitions may not contain the metadata you want to show on the portal item. This sample demonstrates how you can update those properties after publishing a web layer." ] @@ -384,7 +471,7 @@ "outputs": [], "source": [ "data = \"data/power_pedestals_2012.zip\"\n", - "shpfile = gis.content.add({}, data)" + "shpfile = root_folder.add({\"title\": \"power_pedestals_2012\", \"type\": \"Shapefile\"}, data).result()" ] }, { @@ -541,7 +628,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Publish a CSV file and move it into a folder\n", + "## Publish a CSV file and move it into a folder\n", "\n", "To publish a CSV file, we first add the .csv file to the Portal, and then call the publish() method to publish it as a layer. Once published, we create a destination folder on the server and then move the published items into that folder" ] @@ -553,7 +640,7 @@ "outputs": [], "source": [ "csv_file = 'data/Chennai_precipitation.csv'\n", - "csv_item = gis.content.add({}, csv_file)" + "csv_item = root_folder.add({\"title\": \"Chennai Precipitation\", \"type\": \"CSV\"}, csv_file).result()" ] }, { @@ -649,11 +736,14 @@ { "cell_type": "markdown", "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "source": [ "### Create a new folder for the items\n", - "The `create_folder()` from `GIS.content` can be used to create a new folder. Once created, the `move()` of the `Item` can be used to move the items into the folder." + "The `create()` from `GIS.content.folders` can be used to create a new folder. Once created, the `move()` of the `Item` can be used to move the items into the folder." ] }, { @@ -671,8 +761,8 @@ ], "source": [ "# create a new folder called 'Rainfall Data'\n", - "new_folder_details = gis.content.create_folder(my_folder_name)\n", - "print(new_folder_details)" + "new_folder = gis.content.folders.create(my_folder_name)\n", + "print(new_folder)" ] }, { @@ -696,16 +786,19 @@ ], "source": [ "# move both the csv_item and csv_lyr items into this new folder\n", - "csv_item.move(new_folder_details) # Here you could either pass name of the folder or the dictionary\n", + "csv_item.move(new_folder) # Here you could either pass name of the folder or the dictionary\n", " # returned from create_folder() or folders property on a User object\n", "\n", - "csv_lyr.move(new_folder_details)" + "csv_lyr.move(new_folder)" ] }, { "cell_type": "markdown", "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "source": [ "Now that the items are moved, we can request for the item's `ownerFolder` property and ensure it matches the `id` of the folder created in the previous step" @@ -750,7 +843,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.11.10" }, "toc": { "base_numbering": 1, @@ -796,5 +889,5 @@ } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 }