Export Decorator
The export decorator in dessia_common allows you to define custom export methods for your classes, enabling them to be exported in various formats.
Available Export Decorators
@export_html- Export as HTML (text format)@export_json- Export as JSON (text format)@export_step- Export as STEP (text format)@export_stl- Export as STL (binary format)@export_docx- Export as DOCX (text format)@export_xlsx- Export as XLSX (binary format)
Basic Usage
Stream Export (Required)
from dessia_common.decorators import export_html, export_json
from dessia_common.core import DessiaObject
class MyClass(DessiaObject):
@export_html("HTML Export")
def to_html_stream(self, stream):
"""Export object as HTML to a stream."""
stream.write("<html><body>Hello World</body></html>")
@export_json("JSON Export")
def to_json_stream(self, stream):
"""Export object as JSON to a stream."""
stream.write('{"message": "Hello World"}')File Export (Optional)
You can also add a file-based export method using the .file decorator:
class MyClass(DessiaObject):
@export_html("HTML Export")
def to_html_stream(self, stream):
"""Export object as HTML to a stream."""
stream.write("<html><body>Hello World</body></html>")
@to_html_stream.file
def to_html_file(self, filepath):
"""Export object as HTML to a file."""
with open(filepath, "w") as f:
self.to_html_stream(f)Key Points
- Selector: Each export decorator requires a unique selector string (e.g., "HTML Export")
- Stream Method: The decorated method must accept a
streamparameter for writing data - File Method: Optional file-based export using
@method_name.filedecorator - Text vs Binary: Some formats are text-based (HTML, JSON, STEP) while others are binary (STL, XLSX)
- Inheritance: Classes inheriting from
DessiaObjectorModelget automatic access to export functionality
Example
from dessia_common.decorators import export_json
from dessia_common.core import DessiaObject
class Product(DessiaObject):
def __init__(self, name: str, price: float):
super().__init__(name=name)
self.price = price
@export_json("Product Data")
def export_product_data(self, stream):
"""Export product as JSON."""
import json
data = {"name": self.name, "price": self.price}
stream.write(json.dumps(data, indent=2))This enables the Product class to be exported as JSON format with the selector "Product Data".