Prerequisites
Before you begin, make sure you have:- Active sandbox - A running sandbox (see Creating Sandboxes)
- Understanding of code execution - Familiarity with Synchronous Execution is helpful
- Code that generates visualizations - Code that creates plots, DataFrames, or charts
Overview
When you execute code withrun_code(), HopX automatically captures:
- Matplotlib plots → PNG images (base64-encoded)
- Pandas DataFrames → HTML tables
- Plotly charts → HTML interactive visualizations
- JSON outputs → Structured data
Rich output capture is enabled by default in
run_code(). All visual outputs are automatically detected and included in the ExecutionResult.rich_outputs array.Automatic Capture
Rich outputs are captured automatically:- Python
- JavaScript
Matplotlib Plots
Capture matplotlib plots as PNG images:- Python
- JavaScript
Pandas DataFrames
Capture pandas DataFrames as HTML tables:- Python
- JavaScript
Plotly Charts
Capture interactive Plotly charts as HTML:- Python
- JavaScript
Multiple Rich Outputs
Capture multiple outputs in a single execution:- Python
- JavaScript
Rich Output Structure
Each rich output has the following structure:- Python
- JavaScript
Complete Example
Here’s a complete example with all rich output types:- Python
- JavaScript
Best Practices
1
1. Save Files Explicitly
Always save plots to files (e.g.,
plt.savefig()) to ensure they’re captured. Rich output capture works best when files are explicitly saved.2
2. Use Appropriate Formats
- Matplotlib: Save as PNG for best compatibility
- Pandas: Print DataFrames to trigger HTML capture
- Plotly: Use
write_html()for interactive charts
3
3. Check Output Count
Use
len(result.rich_outputs) to verify all expected outputs were captured.4
4. Handle Base64 Encoding
PNG images are base64-encoded. Decode them before displaying or saving locally.
5
5. Multiple Outputs
You can capture multiple plots/DataFrames in a single execution. All will be included in
rich_outputs.Related
- Synchronous Execution - Basic code execution
- Background Execution - Long-running tasks
- SDK: sandbox.run_code() - Python SDK method (rich output enabled by default)
- API: POST /execute - VM Agent API endpoint
- CLI Code Execution - Execute code from CLI
Next Steps
- Learn about Synchronous Execution basics
- Explore Background Execution for long-running visualizations
- Review Streaming Execution for real-time output

