CarboneConnect Python Client
A Python client library for interacting with the Carbone API service. This library provides a simple interface to render documents using templates and data.
Installation
pip install py-carbone-connect
Features
- Template rendering with data
- Support for both file paths and file-like objects as templates
- Streaming response support
- Error handling with custom exceptions
- Automatic content type detection
Usage
Basic Usage
from carbone_connect import CarboneConnect
# Initialize the client
carbone = CarboneConnect('http://selfhosted.carbone-server.com') # it does not have Auth yet
# Render a template with data
template_path = 'path/to/template.docx'
data = {
'name': 'John Doe',
'company': 'ACME Corp'
}
# Generate document
result = carbone.render(template_path, data)
# Save the result
with open('output.docx', 'wb') as f:
f.write(result)
Streaming Response
# Get a streaming response
response = carbone.render_stream(template_path, data)
# Process the stream
with open('output.docx', 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
API Reference
CarboneConnect
__init__(api_url: str)
Initialize the Carbone client with the API URL.
render(template: Union[str, BinaryIO], data: Dict[str, Any], options: Optional[Dict[str, Any]] = None) -> bytes
Render a template with provided data and return the result as bytes.
template: Path to template file or file-like objectdata: Dictionary containing data to render in the templateoptions: Optional rendering options
render_stream(template: Union[str, BinaryIO], data: Dict[str, Any], options: Optional[Dict[str, Any]] = None) -> requests.Response
Render a template and return a streaming response.
Error Handling
The library includes a custom CarboneError exception class for handling API-related errors:
try:
result = carbone.render(template_path, data)
except CarboneError as e:
print(f'Error: {e}')
Test
Install development dependencies
pip install -e .[dev]
Run tests
pytest
Run tests with coverage report
pytest --cov=carbone_connect --cov-report=term-missing
Run specific test
pytest tests/test_client.py -k test_initialization
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Release files for py-carbone-connect 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_carbone_connect-1.1.2.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_carbone_connect-1.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.2 kB
Release files / py_carbone_connect-1.1.2.tar.gz
| Download URL | py_carbone_connect-1.1.2.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e2de8e608a815d544137f8b569a4adc7c9f89305d7e7e0429bfcb8a76c6b4204
|
|
BLAKE2b-256 checksum How to use checksums |
ba52fe2f152988361470c9047706183d9d901b6fb42db4054188a0c1977ea6e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.10
|
Release files / py_carbone_connect-1.1.2-py3-none-any.whl
| Download URL | py_carbone_connect-1.1.2-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4954d02b8b3fa9f941b5f6b4538f5d7e18545c183c944356640458d4d4317a99
|
|
BLAKE2b-256 checksum How to use checksums |
da0a004ab6f3856be2b89fc419b384468ab73692e83c51483875fa54a5bab0d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.10
|