Python examples
These examples combine SPACE Python capabilities into practical workflows.
Short API-specific examples are also included in the relevant feature guides. This page focuses on complete patterns that can be adapted to scripts, notebooks, and automation.
Use a stored API credential
Store an API key as a SPACE secret rather than embedding it in Python code.
Conceptually:
import requests
from space import secrets
credentials = secrets.get("external-api")
response = requests.get(
"https://api.example.com/data",
headers={"Authorization": f"Bearer {credentials['api_key']}"},
)
response.raise_for_status()
data = response.json()The secret remains managed in SPACE and is retrieved only when the Python code runs.
See Secrets and connections for credential-management guidance.
Connect to Planning Analytics with a SPACE connection
Reuse a Planning Analytics connection already configured in SPACE:
from space import connections
from TM1py.Services import TM1Service
config = connections.get("Planning Analytics - Production")
with TM1Service(
base_url=config["base_url"],
user=config["username"],
password=config["password"],
namespace=config["namespace"],
) as tm1:
print(tm1.server.get_server_name())
print(tm1.server.get_product_version())The exact fields depend on the connection's authentication type.
This pattern avoids maintaining a separate space.cfg or hard-coded Planning Analytics credentials for the Python project.
Connect to Planning Analytics with a secret
When a Python workflow needs credentials that are not represented by a configured SPACE connection, store them as a secret:
from space import secrets
from TM1py.Services import TM1Service
credentials = secrets.get("tm1-automation")
with TM1Service(
address="tm1.example.com",
port=12354,
ssl=True,
user=credentials["username"],
password=credentials["password"],
namespace="LDAP",
) as tm1:
print(tm1.server.get_server_name())This keeps the username and password out of the script while allowing non-sensitive connection settings to remain explicit.
Query TM1 with TM1py
Once connected, use TM1py for Planning Analytics operations:
mdx = '''
SELECT
{[Period].[Period].[2026]} ON COLUMNS,
{[Region].[Region].[North America]} ON ROWS
FROM [Sales]
WHERE ([Version].[Version].[Actual])
'''
df = tm1.cells.execute_mdx_dataframe(mdx)
print(df)A notebook is useful while developing and inspecting the result. When the workflow is stable, the same logic can be moved into a .py file for reuse or automation.
Execute a TI process from Python
TM1py can execute an existing TurboIntegrator process and pass its parameters:
success, status, error_log_file = tm1.processes.execute_with_return(
"Load Actuals",
pYear="2026",
pMonth="10",
)
if not success:
raise RuntimeError(f"TI process failed: {status}")This pattern can be used as part of a larger Python workflow that performs preparation or validation before and after a Planning Analytics process runs.
Turn an interactive workflow into a scheduled job
A practical development flow is:
- prototype the logic in a Jupyter notebook;
- move reusable code into a Python script;
- replace hard-coded credentials with SPACE-managed secrets or connections;
- test the script interactively;
- configure a Python script job under Settings → Jobs.
This lets the same Python logic move from exploration to repeatable automation without maintaining a separate execution environment.
Keep Python projects in Git
Store reusable scripts, notebooks, and related project files in the Python Git repository so changes can be reviewed and versioned.
See Git for the JupyterLab Git workflow and for SSH configuration on client-managed SPACE installations.
