|
| 1 | +# plotly_static |
| 2 | + |
| 3 | +Export Plotly plots to static images using WebDriver and headless browsers. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`plotly_static` provides a Rust interface for converting Plotly plots from JSON data into various static image formats (PNG, JPEG, WEBP, SVG, PDF) using WebDriver and headless browsers. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Multiple Formats**: PNG, JPEG, WEBP, SVG, PDF |
| 12 | +- **Browser Support**: Chrome/Chromium (chromedriver) and Firefox (geckodriver) |
| 13 | +- **Efficient**: Reuse `StaticExporter` instances for multiple exports |
| 14 | +- **String Export**: Base64 and SVG output for web applications |
| 15 | +- **Parallel Safe**: Designed for concurrent usage |
| 16 | +- **Automatic Management**: Handles WebDriver lifecycle and cleanup |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +```rust |
| 21 | +use plotly_static::{StaticExporterBuilder, ImageFormat}; |
| 22 | +use serde_json::json; |
| 23 | +use std::path::Path; |
| 24 | + |
| 25 | +// Create a simple plot as JSON |
| 26 | +let plot = json!({ |
| 27 | + "data": [{ |
| 28 | + "type": "scatter", |
| 29 | + "x": [1, 2, 3, 4], |
| 30 | + "y": [10, 11, 12, 13] |
| 31 | + }], |
| 32 | + "layout": { |
| 33 | + "title": "Simple Scatter Plot" |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +// Build and use StaticExporter |
| 38 | +let mut exporter = StaticExporterBuilder::default() |
| 39 | + .build() |
| 40 | + .expect("Failed to build StaticExporter"); |
| 41 | + |
| 42 | +// Export to PNG |
| 43 | +exporter.write_fig( |
| 44 | + Path::new("my_plot"), |
| 45 | + &plot, |
| 46 | + ImageFormat::PNG, |
| 47 | + 800, |
| 48 | + 600, |
| 49 | + 1.0 |
| 50 | +).expect("Failed to export plot"); |
| 51 | +``` |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +Add to your `Cargo.toml`: |
| 56 | + |
| 57 | +```toml |
| 58 | +[dependencies] |
| 59 | +plotly_static = { version = "0.0.1", features = ["chromedriver", "webdriver_download"] } |
| 60 | +serde_json = "1.0" |
| 61 | +``` |
| 62 | + |
| 63 | +### Feature Flags |
| 64 | + |
| 65 | +- `chromedriver`: Use Chromedriver and Chrome/Chromium browser for rendering and export |
| 66 | +- `geckodriver`: Use Geckodriver Firefox browser for rendering for rendering and export |
| 67 | +- `webdriver_download`: Auto-download the chosen WebDriver binary |
| 68 | + |
| 69 | +## Prerequisites |
| 70 | + |
| 71 | +1. **Browser**: Chrome/Chromium or Firefox installed |
| 72 | +2. **WebDriver**: Manually installed or automatically downloaded and installed with the `webdriver_download` feature |
| 73 | +3. **Internet Connectivity**: Required for WebDriver download when using the auto-download and install feature |
| 74 | + |
| 75 | +## Advanced Usage |
| 76 | + |
| 77 | +### Static Exporter Reuse |
| 78 | + |
| 79 | +```rust |
| 80 | +use plotly_static::{StaticExporterBuilder, ImageFormat}; |
| 81 | +use serde_json::json; |
| 82 | +use std::path::Path; |
| 83 | + |
| 84 | +let plot1 = json!({ |
| 85 | + "data": [{"type": "scatter", "x": [1,2,3], "y": [4,5,6]}], |
| 86 | + "layout": {"title": "Plot 1"} |
| 87 | +}); |
| 88 | + |
| 89 | +let plot2 = json!({ |
| 90 | + "data": [{"type": "scatter", "x": [2,3,4], "y": [5,6,7]}], |
| 91 | + "layout": {"title": "Plot 2"} |
| 92 | +}); |
| 93 | + |
| 94 | +let mut exporter = StaticExporterBuilder::default() |
| 95 | + .build() |
| 96 | + .expect("Failed to create StaticExporter"); |
| 97 | + |
| 98 | +// Reuse for multiple exports |
| 99 | +exporter.write_fig(Path::new("plot1"), &plot1, ImageFormat::PNG, 800, 600, 1.0)?; |
| 100 | +exporter.write_fig(Path::new("plot2"), &plot2, ImageFormat::JPEG, 800, 600, 1.0)?; |
| 101 | +``` |
| 102 | + |
| 103 | +### String Export |
| 104 | + |
| 105 | +```rust |
| 106 | +use plotly_static::{StaticExporterBuilder, ImageFormat}; |
| 107 | +use serde_json::json; |
| 108 | + |
| 109 | +let plot = json!({ |
| 110 | + "data": [{"type": "scatter", "x": [1,2,3], "y": [4,5,6]}], |
| 111 | + "layout": {} |
| 112 | +}); |
| 113 | + |
| 114 | +let mut exporter = StaticExporterBuilder::default() |
| 115 | + .build() |
| 116 | + .expect("Failed to create StaticExporter"); |
| 117 | + |
| 118 | +// Get base64 data for web embedding |
| 119 | +let base64_data = exporter.write_to_string(&plot, ImageFormat::PNG, 400, 300, 1.0)?; |
| 120 | + |
| 121 | +// Get SVG data (vector format) |
| 122 | +let svg_data = exporter.write_to_string(&plot, ImageFormat::SVG, 400, 300, 1.0)?; |
| 123 | +``` |
| 124 | + |
| 125 | +### Custom Configuration |
| 126 | + |
| 127 | +```rust |
| 128 | +use plotly_static::StaticExporterBuilder; |
| 129 | + |
| 130 | +let mut exporter = StaticExporterBuilder::default() |
| 131 | + .webdriver_port(4445) // Unique port for parallel usage |
| 132 | + .offline_mode(true) // Use bundled JavaScript |
| 133 | + .webdriver_browser_caps(vec![ |
| 134 | + "--headless".to_string(), |
| 135 | + "--no-sandbox".to_string(), |
| 136 | + ]) |
| 137 | + .build()?; |
| 138 | +``` |
| 139 | + |
| 140 | +## Environment Variables |
| 141 | + |
| 142 | +- `WEBDRIVER_PATH`: Custom WebDriver binary location |
| 143 | +- `BROWSER_PATH`: Custom browser binary location |
| 144 | + |
| 145 | +## Examples |
| 146 | + |
| 147 | +Check the self contatined examples in the examples folder. |
| 148 | + |
| 149 | +Similar examples are available in the [Plotly.rs package](https://github.com/plotly/plotly.rs), in [Plotly.rs Book](https://plotly.github.io/plotly.rs/) as well as the example in [Plotly.rs examples/static_export](https://github.com/plotly/plotly.rs/tree/main/examples/static_export). |
| 150 | + |
| 151 | +## Documentation |
| 152 | + |
| 153 | +- [API Documentation](https://docs.rs/plotly_static/) |
| 154 | +- [Static Image Export Guide](../../docs/book/src/fundamentals/static_image_export.md) |
| 155 | + |
| 156 | +## License |
| 157 | + |
| 158 | +This package is licensed under the MIT License. |
0 commit comments