Creating Tags
Problem
You want to organize content using tags.
Solution
Create tags, which can then be assigned to content items (see Assigning tags).
You must have administrator privileges to create tags.
First, create a tag category.
A tag created without a parent reference is considered a tag category. Tag categories serve as the top-level category for organizing content. We cannot directly assign a tag category to content. Instead, we can use a tag category as the parent for an individual tag. This approach allows for grouping tags that are related together.
from posit import connect
= "Departments"
TAG_CATEGORY_NAME
= connect.Client()
client = client.post("/v1/tags", json={
response "name": "Departments",
})
= response.json() departments
The departments
object contains additional information about the tag category, including the id
field. We will use the id
field to create a tag in the next step.
>>> import polars as pl
>>> pl.DataFrame(tag_category)
1, 5)
shape: (
┌─────┬─────────────┬───────────┬──────────────────────┬──────────────────────┐id ┆ name ┆ parent_id ┆ created_time ┆ updated_time │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ null ┆ str ┆ str │
│
╞═════╪═════════════╪═══════════╪══════════════════════╪══════════════════════╡100 ┆ Departments ┆ null ┆ 2024-06-20T14:25:20Z ┆ 2024-06-20T14:25:20Z │
│ └─────┴─────────────┴───────────┴──────────────────────┴──────────────────────┘
library(connectapi)
= "Departments"
TAG_CATEGORY_NAME
<- connect()
client create_tag(client, "TAG_CATEGORY_NAME")
Then get the tag information using the get_tags
function.
<- get_tags(client)
tags <- tags$Departments departments
We can print the departments
object to view the tag tree.
> departments
Tree (filtered)
Posit Connect Tag └── Departments
Next, create a tag and assign it to the tag category.
See Viewing Tag Information for information on obtaining the id
field for an existing tag.
You can repeat the following example multiple times to create sub-categories of an arbitrary depth. Try creating another tag named “Human Resources” with the parent “Administrative”.
= departments['id']
PARENT_TAG_ID = "Administrative"
TAG_NAME
= client.post("/v1/tags", json={
res "name": TAG_NAME,
"parent_id": PARENT_TAG_ID
})
= response.json() administrative
The administrative
object contains a unique identifier (id
) and and a parent_id
, which is a reference to the parent via the provided PARENT_TAG_ID
.
>>> pl.DataFrame(administrative)
1, 5)
shape: (
┌─────┬────────────────┬───────────┬──────────────────────┬──────────────────────┐id ┆ name ┆ parent_id ┆ created_time ┆ updated_time │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ str ┆ str │
│
╞═════╪════════════════╪═══════════╪══════════════════════╪══════════════════════╡200 ┆ Administrative ┆ 100 ┆ 2024-06-20T14:27:22Z ┆ 2024-06-20T14:27:22Z │
│ └─────┴────────────────┴───────────┴──────────────────────┴──────────────────────┘
We use the parent
argument to assign the new tag to the departments
tag category.
= "Administrative"
TAG_NAME create_tag(client, name = TAG_NAME, parent = departments)
Get the tag information again using the get_tags
function to view the updated tag tree.
> tags <- get_tags(client)
> tags$Departments
Tree (filtered)
Posit Connect Tag
Departments
└── Administrative> tags$Departments$Administrative
Tree (filtered)
Posit Connect Tag └── Administrative
Next, see Assigning Tags to learn how to assign a tag to content.