Get Started with Python Utils Library
This guide will help you get started with Contentstack Python Utils SDK to build apps powered by Contentstack.
Prerequisites
- The latest version of PyCharm or Visual Studio Code
- Python 3
- An activated virtual environment for the project
SDK Installation and Setup
To set up Python Utils SDK, go to the terminal and locate the virtual environment path. Run the command given below:
- macOS
- Windows
python3 -m pip install contentstack_utils
py -m pip install contentstack_utils
from contentstack_utils.render.options import Options
class DemoOption(Options):
def render_options(self, _obj, metadata):
if metadata.style_type == 'block':
return '<p>' + _obj['title'] + '</p><span>' + _obj['multi'] + '</span>'
if metadata.style_type == 'inline':
return '<p>' + _obj['title'] + '</p><span>' + _obj['line'] + '</span>'
if metadata.style_type == 'link':
return '<p>' + _obj['title'] + '</p><span>' + _obj['key'] + '</span>'
if metadata.style_type == 'display':
return '<p>' + _obj['title'] + '</p><span>' + _obj['multi'] + '</span>'
def render_mark(self, mark_type, render_text) -> str:
if mark_type == 'bold':
return '<b>' + render_text + '</b>'
else:
return ''
def render_node(self, node_type, node_obj: dict, callback):
if node_type == 'paragraph':
children = callback(node_obj['children'])
return "<p class='class-id'>" + children + '</p>'
else:
return ''Basic Queries
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
Fetch Embedded Item(s) from a Single Entry:
To get an embedded item of a single entry, you need to provide the stack API key, environment name, content type’s UID, and entry’s UID. Then, use the entry.fetch function as shown below:
import contentstack
from contentstack_utils.render.options import Options
stack = contentstack.Stack('api_key','delivery_token','environment')
content_type = stack.content_type("content_type_uid")
entry = content_type.entry("entry_uid")
result = entry.fetch()
if result is not None:
entry = result['entries']
Utils.render(entry, ['rich_text_editor', 'some_other_text'], Options())Fetch Embedded Item(s) from Multiple Entries
To get embedded items from multiple entries, you need to provide the stack API key, delivery token, environment name, and content type’s UID.
import contentstack
from contentstack_utils.utils import Utils
from contentstack_utils.render.options import Options
stack = contentstack.Stack('api_key','delivery_token','environment')
query = stack.content_type("content_type_uid").query()
result = query.find()
if result is not None and 'entries' in result:
entry = result['entries']
for item in range:
Utils.render(item, ['rich_text_editor', 'some_other_text'], Options())Render JSON RTE Contents
To get multiple entries, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the Utils.json_to_html function as shown below:
from contentstack_utils.utils import Utils from contentstack_utils.render.options import Options path = [‘content_path_one’, ‘content_path_2’] # should be type of dictionary or list entry_content = "html_string" response = Utils.json_to_html(entry_content, path, Options()) print(response)
Render GQL SRTE Contents
To get multiple entries, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the GQL.json_to_html function as shown below:
from contentstack_utils.gql import GQL
from contentstack_utils.render.options import Options
path = ['content_path_one', 'content_path_2'] # should be type of dictionary or list
entry_content = "html_string"
response = GQL.json_to_html(entry_content, path, Options())
print(response)Resolve Embedded Item Metadata
The SDK resolves embedded entry and asset metadata from the _embedded_items object in the API response and exposes the resolved values at node.attrs._resolved. Read resolved values from there so that your rendered output reflects the current state of the embedded item. The legacy node.attrs['asset-link'] property, and the equivalent properties for other node types, remain readable as a soft-deprecated fallback.
Note The Content Delivery SDK retrieves first-level embedded items only. To retrieve embedded items that are nested inside other embedded items, request the entry directly through the Content Delivery API with include_embedded_items[]=RECURSIVE.
Additional Resources
- Refer to Embed Entries or Assets to understand how embedded item data is stored and resolved.
- Refer to CDA | Entries for the include_embedded_items[] and embedded_items_depth parameter reference.