Skip to content

Commit 00fc82c

Browse files
committed
add own readme to plotly_static
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 0a46de0 commit 00fc82c

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
2424
- [[#314](https://github.com/plotly/plotly.rs/pull/314)] Add axis range bounds support
2525
- [[#317](https://github.com/plotly/plotly.rs/pull/317)] Show rangebreak usage with ints
2626
- [[#318](https://github.com/plotly/plotly.rs/pull/318)] Add slider support
27+
- [[#319](https://github.com/plotly/plotly.rs/pull/319)] Introduce `plotly_static` package for static export of plots using WebDriver driven browsers
28+
2729

2830
### Fixed
2931
- [[#284](https://github.com/plotly/plotly.rs/pull/284)] Allow plotly package to be compiled for android

plotly_static/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Plotly, Inc
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

plotly_static/README.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)