|
| 1 | +# ADCP Python Client Examples |
| 2 | + |
| 3 | +This directory contains examples demonstrating how to use the ADCP Python client's preview URL generation feature. |
| 4 | + |
| 5 | +## Preview URL Generation Demo |
| 6 | + |
| 7 | +This demo shows how to fetch creative format previews from the ADCP creative agent and display them in a web browser using the `<rendered-creative>` web component. |
| 8 | + |
| 9 | +### Quick Start |
| 10 | + |
| 11 | +1. **Install the package** (if not already installed): |
| 12 | + ```bash |
| 13 | + pip install -e . |
| 14 | + ``` |
| 15 | + |
| 16 | +2. **Fetch preview URLs from the creative agent**: |
| 17 | + ```bash |
| 18 | + python examples/fetch_previews.py |
| 19 | + ``` |
| 20 | + |
| 21 | + This will: |
| 22 | + - Connect to the reference creative agent at `https://creative.adcontextprotocol.org` |
| 23 | + - Call `list_creative_formats()` with `fetch_previews=True` |
| 24 | + - Generate preview URLs for available formats |
| 25 | + - Save the results to `examples/preview_urls.json` |
| 26 | + |
| 27 | +3. **Start a local web server** (required for the web component to work): |
| 28 | + ```bash |
| 29 | + cd /path/to/adcp-client-python |
| 30 | + python -m http.server 8000 |
| 31 | + ``` |
| 32 | + |
| 33 | +4. **Open the demo in your browser**: |
| 34 | + ``` |
| 35 | + http://localhost:8000/examples/web_component_demo.html |
| 36 | + ``` |
| 37 | + |
| 38 | +### What You'll See |
| 39 | + |
| 40 | +The demo page displays a grid of creative format previews, each showing: |
| 41 | +- Format name |
| 42 | +- Format ID |
| 43 | +- Live preview rendered in the `<rendered-creative>` web component |
| 44 | + |
| 45 | +Features demonstrated: |
| 46 | +- **Shadow DOM isolation** - No CSS conflicts between previews |
| 47 | +- **Lazy loading** - Previews load only when visible |
| 48 | +- **Responsive grid** - Adapts to different screen sizes |
| 49 | +- **Interactive previews** - Full creative rendering with animations |
| 50 | + |
| 51 | +### Files |
| 52 | + |
| 53 | +- **`fetch_previews.py`** - Python script to fetch preview URLs from the creative agent |
| 54 | +- **`web_component_demo.html`** - HTML page demonstrating the `<rendered-creative>` web component |
| 55 | +- **`preview_urls.json`** - Generated file containing preview URLs (created by fetch script) |
| 56 | + |
| 57 | +### How It Works |
| 58 | + |
| 59 | +The Python script uses the new `fetch_previews` parameter: |
| 60 | + |
| 61 | +```python |
| 62 | +from adcp import ADCPClient |
| 63 | +from adcp.types import AgentConfig, Protocol |
| 64 | +from adcp.types.generated import ListCreativeFormatsRequest |
| 65 | + |
| 66 | +creative_agent = ADCPClient( |
| 67 | + AgentConfig( |
| 68 | + id="creative_agent", |
| 69 | + agent_uri="https://creative.adcontextprotocol.org", |
| 70 | + protocol=Protocol.MCP, |
| 71 | + ) |
| 72 | +) |
| 73 | + |
| 74 | +# Fetch formats with preview URLs |
| 75 | +result = await creative_agent.list_creative_formats( |
| 76 | + ListCreativeFormatsRequest(), |
| 77 | + fetch_previews=True # ← New parameter! |
| 78 | +) |
| 79 | + |
| 80 | +# Access preview data |
| 81 | +formats_with_previews = result.metadata["formats_with_previews"] |
| 82 | +for fmt in formats_with_previews: |
| 83 | + preview_data = fmt["preview_data"] |
| 84 | + print(f"Preview URL: {preview_data['preview_url']}") |
| 85 | + print(f"Expires: {preview_data['expires_at']}") |
| 86 | +``` |
| 87 | + |
| 88 | +The HTML page then uses the official ADCP web component to render the previews: |
| 89 | + |
| 90 | +```html |
| 91 | +<!-- Include the web component --> |
| 92 | +<script src="https://creative.adcontextprotocol.org/static/rendered-creative.js"></script> |
| 93 | + |
| 94 | +<!-- Render previews --> |
| 95 | +<rendered-creative |
| 96 | + src="{{preview_url}}" |
| 97 | + width="300" |
| 98 | + height="400" |
| 99 | + lazy="true"> |
| 100 | +</rendered-creative> |
| 101 | +``` |
| 102 | + |
| 103 | +### Troubleshooting |
| 104 | + |
| 105 | +**"Preview URLs file not found" error:** |
| 106 | +- Run `python examples/fetch_previews.py` first |
| 107 | + |
| 108 | +**Web component not loading:** |
| 109 | +- Make sure you're using a web server (not `file://` URLs) |
| 110 | +- Check that you have internet access (web component loads from CDN) |
| 111 | + |
| 112 | +**No previews showing:** |
| 113 | +- Check browser console for errors |
| 114 | +- Verify the creative agent is accessible: `https://creative.adcontextprotocol.org` |
| 115 | + |
| 116 | +### Using with Products |
| 117 | + |
| 118 | +You can also fetch preview URLs for products: |
| 119 | + |
| 120 | +```python |
| 121 | +from adcp.types.generated import GetProductsRequest |
| 122 | + |
| 123 | +# Setup publisher and creative agents |
| 124 | +publisher_agent = ADCPClient(publisher_config) |
| 125 | +creative_agent = ADCPClient(creative_config) |
| 126 | + |
| 127 | +# Get products with preview URLs |
| 128 | +result = await publisher_agent.get_products( |
| 129 | + GetProductsRequest(brief="video campaign"), |
| 130 | + fetch_previews=True, |
| 131 | + creative_agent_client=creative_agent |
| 132 | +) |
| 133 | + |
| 134 | +# Access product previews |
| 135 | +products_with_previews = result.metadata["products_with_previews"] |
| 136 | +for product in products_with_previews: |
| 137 | + print(f"Product: {product['name']}") |
| 138 | + for format_id, preview_data in product["format_previews"].items(): |
| 139 | + print(f" Format {format_id}: {preview_data['preview_url']}") |
| 140 | +``` |
| 141 | + |
| 142 | +## More Examples |
| 143 | + |
| 144 | +For more examples, see the [documentation](https://docs.adcontextprotocol.org) and [integration tests](../tests/integration/). |
0 commit comments