Get Started with Python SDK and Live Preview
This guide will help you get started with Contentstack Python SDK to build apps powered by Contentstack.
Pre-requisites
Installation and Setup
To use the Contentstack Python SDK with your existing project, perform the following steps:
Open the terminal, create a project, and move inside it as follows:
mkdir project_name cd project_name
Create a virtual environment:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
Install pip Contentstack as follows:
pip install contentstack
You can download the latest dependency version here.
Initializing the Stack with Live Preview
Since the Live Preview Utils SDK is responsible for communication, you need to initialize it within your stack.
Use the following command to initialize the stack:
stack = contentstack.Stack('api_key', 'delivery_token', 'environment', live_preview= {
'enable': True,
'authorization': 'management_token',
'host': 'api.contentstack.io',
});
Note: By default, the host parameter points to the North America endpoint. If your website is hosted on the European data center, then pass the European endpoint against the host parameter.
Create an Interceptor using Flask
Use the following code to create an interceptor using Flask:
from flask import request
from flask import Flask
app = Flask(__name__)
@app.before_request
def before_request():
request_str = str(request.query_string)
stack.live_preview_query(request_str)
For Server-side Rendered Websites
To install and initialize the Live Preview Utils SDK, you can refer to our SSR Live Preview Setup documentation.
Query Request
Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.
To get an entry, you need to specify the content type UID and the UID of the entry.
entry = stack.content_type("content_type_uid").entry();
result = entry.fetch()