Template Catalog Module
Use Python to programmatically interact with the PraisonAI template catalog - fetch templates, search, filter, and integrate with your applications.Fetching Templates
Fetch the template catalog JSON from the deployed catalog site or local build.Copy
import json
import urllib.request
# Fetch from deployed catalog
CATALOG_URL = "https://mervinpraison.github.io/praisonai-template-catalog/data/templates.json"
def fetch_templates():
"""Fetch templates from the catalog."""
with urllib.request.urlopen(CATALOG_URL) as response:
data = json.loads(response.read().decode())
return data["templates"]
# Get all templates
templates = fetch_templates()
print(f"Found {len(templates)} templates")
# Print template names
for t in templates:
print(f"- {t['name']} (v{t['version']})")
Searching Templates
Search templates by name, description, or tags.Copy
def search_templates(templates, query):
"""Search templates by query string."""
query = query.lower()
results = []
for t in templates:
# Search in name, description, and tags
if (query in t["name"].lower() or
query in t.get("description", "").lower() or
any(query in tag.lower() for tag in t.get("tags", []))):
results.append(t)
return results
# Search for video templates
video_templates = search_templates(templates, "video")
print(f"Found {len(video_templates)} video templates:")
for t in video_templates:
print(f" - {t['name']}: {t['description'][:50]}...")
Filtering by Tags
Filter templates by specific tags.Copy
def filter_by_tags(templates, tags):
"""Filter templates that have all specified tags."""
return [
t for t in templates
if all(tag in t.get("tags", []) for tag in tags)
]
# Find templates with both 'video' and 'editing' tags
editing_templates = filter_by_tags(templates, ["video", "editing"])
for t in editing_templates:
print(f"{t['name']}: {t['tags']}")
Filtering by Requirements
Filter templates by their tool or package requirements.Copy
def filter_by_tool(templates, tool_name):
"""Filter templates that require a specific tool."""
return [
t for t in templates
if tool_name in t.get("requires", {}).get("tools", [])
]
def filter_by_package(templates, package_name):
"""Filter templates that require a specific package."""
return [
t for t in templates
if package_name in t.get("requires", {}).get("packages", [])
]
# Find templates using shell_tool
shell_templates = filter_by_tool(templates, "shell_tool")
print(f"Templates using shell_tool: {[t['name'] for t in shell_templates]}")
# Find templates requiring moviepy
moviepy_templates = filter_by_package(templates, "moviepy")
print(f"Templates requiring moviepy: {[t['name'] for t in moviepy_templates]}")
Getting Template Details
Get detailed information about a specific template.Copy
def get_template(templates, name):
"""Get a template by name."""
for t in templates:
if t["name"] == name:
return t
return None
# Get ai-video-editor details
template = get_template(templates, "ai-video-editor")
if template:
print(f"Name: {template['name']}")
print(f"Version: {template['version']}")
print(f"Description: {template['description']}")
print(f"Author: {template.get('author', 'Unknown')}")
print(f"Tags: {', '.join(template.get('tags', []))}")
print(f"Required tools: {template.get('requires', {}).get('tools', [])}")
print(f"Required packages: {template.get('requires', {}).get('packages', [])}")
print(f"Required env vars: {template.get('requires', {}).get('env', [])}")
Generating CLI Commands
Generate CLI commands for templates.Copy
def generate_cli_commands(template):
"""Generate CLI commands for a template."""
name = template["name"]
return {
"run": f"praisonai templates run {name}",
"info": f"praisonai templates info {name}",
"init": f"praisonai templates init my-project --template {name}",
}
# Generate commands for a template
template = get_template(templates, "transcript-generator")
if template:
commands = generate_cli_commands(template)
print("CLI Commands:")
for cmd_type, cmd in commands.items():
print(f" {cmd_type}: {cmd}")
Checking Dependencies
Check if template dependencies are satisfied.Copy
import subprocess
import os
def check_dependencies(template):
"""Check if template dependencies are satisfied."""
requires = template.get("requires", {})
results = {
"tools": {},
"packages": {},
"env": {},
"all_satisfied": True
}
# Check packages
for pkg in requires.get("packages", []):
try:
__import__(pkg.replace("-", "_"))
results["packages"][pkg] = True
except ImportError:
results["packages"][pkg] = False
results["all_satisfied"] = False
# Check environment variables
for env_var in requires.get("env", []):
results["env"][env_var] = bool(os.environ.get(env_var))
if not results["env"][env_var]:
results["all_satisfied"] = False
return results
# Check dependencies for a template
template = get_template(templates, "ai-video-editor")
if template:
deps = check_dependencies(template)
print(f"All satisfied: {deps['all_satisfied']}")
print(f"Packages: {deps['packages']}")
print(f"Environment: {deps['env']}")
Using with PraisonAI Templates Module
Use the built-in templates module for more features.Copy
from praisonai.templates import (
load_template,
search_templates,
list_templates
)
# List all templates
templates = list_templates()
for t in templates:
print(f"{t.name} - {t.description[:50]}...")
# Search templates
results = search_templates("video")
print(f"Found {len(results)} video templates")
# Load a specific template
template = load_template("transcript-generator")
print(f"Loaded: {template.name}")
print(f"Tools required: {template.requires.get('tools', [])}")
Running Templates Programmatically
Run templates from Python code.Copy
from praisonai.templates import load_template
from praisonai.templates.loader import TemplateLoader
# Load and run a template
loader = TemplateLoader()
template = loader.load("transcript-generator")
# Check requirements first
missing = loader.check_requirements(template)
if missing["missing_packages"]:
print(f"Missing packages: {missing['missing_packages']}")
if missing["missing_env"]:
print(f"Missing env vars: {missing['missing_env']}")
# Load workflow config
workflow_config = loader.load_workflow_config(template)
print(f"Workflow process: {workflow_config.get('process', 'sequential')}")
Complete Example
A complete example that fetches, searches, and displays templates.Copy
#!/usr/bin/env python3
"""Example: Fetch and search PraisonAI templates."""
import json
import urllib.request
from typing import List, Dict, Any
CATALOG_URL = "https://mervinpraison.github.io/praisonai-template-catalog/data/templates.json"
def fetch_catalog() -> Dict[str, Any]:
"""Fetch the template catalog."""
with urllib.request.urlopen(CATALOG_URL) as response:
return json.loads(response.read().decode())
def search(templates: List[Dict], query: str) -> List[Dict]:
"""Search templates."""
query = query.lower()
return [
t for t in templates
if query in t["name"].lower() or
query in t.get("description", "").lower() or
any(query in tag for tag in t.get("tags", []))
]
def main():
# Fetch catalog
print("Fetching template catalog...")
catalog = fetch_catalog()
templates = catalog["templates"]
print(f"Catalog version: {catalog['version']}")
print(f"Total templates: {catalog['count']}")
# Search for video templates
print("\nSearching for 'video' templates...")
results = search(templates, "video")
for t in results:
print(f"\n{t['name']} (v{t['version']})")
print(f" {t['description'][:80]}...")
print(f" Tags: {', '.join(t.get('tags', []))}")
print(f" Run: praisonai templates run {t['name']}")
if __name__ == "__main__":
main()

