ArcGIS Blog

Announcements

ArcGIS Hub

Introducing ArcGIS Hub Python API for Sites

By Manushi Majumdar

Sites is a website builder included in ArcGIS Hub and Enterprise. It allows you to create engaging and informative sites and pages for sharing open data, connecting with your community, and informing everyone of projects, initiatives, and events. To learn more about Sites, click here.

Hub’s Python API (arcgishub) is a Python library that allows automation of several Hub worksflows and simplifies the use of the Hub information model. It is built over the ArcGIS API for Python.

arcgishub now equips users with tools needed to work with the sites in your Hub or Enterprise organization. You can create new sites and pages, search for existing sites and pages, as well as edit their layout and clone them, not only within the same organization, but over to different organizations as well. Sounds exciting? Let’s walk through a few examples.

Before you get started, you will need to install the library. Read through the steps to install. You start by signing into your Hub/Enterprise organization:

login

Creating initiative and site

If your organization has licensed ArcGIS Hub Premium, you have access to the premium feature of Initiatives. We will start by creating an initiative and accessing the site created along with it.

new_initiative

And to access the site of the initiative:

new_site

However, if you are using Hub Basic or ArcGIS Enterprise, you can connect to an instance of Hub in the same way, and create a site as shown below:

basic_site

Pages of a Site

To add a new page to your existing site:

new_page

Hub’s Python API allows you to link existing pages to your site, with an optional new slug specific to the site.

link_page

Editing Layout of a Site

The Site Editor provides a user with several capabilities in the form of cards to simplify their site building and editing workflows. However, applying a common change to multiple sites and pages in your organization can become a tedious process.
Using the site layout editing functionality supported in Hub’s Python API, you can successfully apply a common change across all the necessary sites and pages.

In order to programmatically edit a site, we need to take a deeper dive into the data model of a Site/Page item. The site layout has Sections which contain Rows within, and the row contain Cards within.

Sections have configurable attributes such as background color and font-color and are used to theme a site or page. Rows are the building blocks of your site are implicitly created when you add a card to a section below another card. Whenever you want to add a card, such as a text card or image card, you must have a row card positioned where you want to add the content. Multiple cards can fit in a row card.

site_layout

We will now see how these components of a site can be accessed using our Python API.

site_layout_access

As we see in the image above, a section could have many rows, and a row could have one or many cards (as seen in the second row with 3 statistics cards).

Now that we understand this layout, editing it using the API is a breeze! Let’s take a look at a few examples below.

To add a new section, row or card to the existing layout, you use the append list method of Python, with the dictionary for the new section, row or card to be added.

add_row

For editing a particular element of the site layout, you access the particular element and make changes to the layout object as shown below

edit_header

To delete a new section, row or card from the existing layout, you use the `pop` list method of Python, with the index for the section, row or card to be deleted.

delete_section

However, to overwrite these changes to the site item and modify the site layout, you can use the update_layout method on the site object with the modified layout.

publish_layout

Cloning a Site

Have you ever created a site you liked enough to want to create copies of that site for reuse? Cloning the site within the same organization is a quick and easy process using our Python API.
Based on your ArcGIS Hub license, you can clone the initiative or site. For a Hub Premium organization, you can clone the initiative and access its site as follows:

 

initiative_clone

And cloning a site for Hub Basic or Enterprise organizations can be achieved using:

site_clone

Using Hub’s Python API, you can also clone a site from one organization to another, provided it is a public site or you have access to the site from the other organization.
We will see an example of cloning the site we created previously in a Hub Basic organization to a Hub Premium organization. Notice that it creates the initiative for the site as well, per the added features of the organization.

cloned_from_basic

Similarly, you can clone sites between Enterprise, Hub Basic or Hub Premium organizations with ease.

These are just a few examples of customizing your sites and pages using Python scripts. For more workflows, we have sample Jupyter notebooks with more examples for sites, pages and more in out GitHub repository.

I invite you to explore the sites module of ArcGIS Hub’s Python API and would love to hear your feedback. Feel free to connect with us on on GeoNet or report issues in our GitHub Repository.

Here’s to building a lovable Hub for Developers!

Share this article

Subscribe
Notify of
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments