Skip to content

Commit 8755be0

Browse files
committed
refactor and add ci step for publishing plotly_static
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent db67cdc commit 8755be0

File tree

12 files changed

+245
-99
lines changed

12 files changed

+245
-99
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
# Script to build examples needed for book artifacts
4+
# This script is used by both the CI build_book job and the book.yml workflow
5+
6+
# Note: static_export example requires Chrome setup for WebDriver functionality
7+
# The CI jobs set up Chrome, while when running locally, you may need to setup
8+
# your browser and webdriver manually to ensure this example works properly.
9+
10+
set -e # Exit on any error
11+
12+
# Function to display usage
13+
usage() {
14+
echo "Usage: $0 <examples_directory>"
15+
echo " examples_directory: Path to the examples directory (e.g., examples or \$GITHUB_WORKSPACE/examples)"
16+
echo ""
17+
echo "This script builds all examples needed for the book documentation."
18+
exit 1
19+
}
20+
21+
# Check if examples directory is provided
22+
if [ $# -ne 1 ]; then
23+
usage
24+
fi
25+
26+
EXAMPLES_DIR="$1"
27+
28+
# Validate that the examples directory exists
29+
if [ ! -d "$EXAMPLES_DIR" ]; then
30+
echo "Error: Examples directory '$EXAMPLES_DIR' does not exist"
31+
exit 1
32+
fi
33+
34+
echo "Building examples for book artifacts from: $EXAMPLES_DIR"
35+
36+
# List of examples needed for the book, sorted alphabetically
37+
# These examples generate HTML files that are included in the book documentation
38+
BOOK_EXAMPLES=(
39+
"3d_charts"
40+
"basic_charts"
41+
"custom_controls"
42+
"financial_charts"
43+
"scientific_charts"
44+
"shapes"
45+
"static_export"
46+
"statistical_charts"
47+
"subplots"
48+
"themes"
49+
)
50+
51+
# Build each example
52+
for example in "${BOOK_EXAMPLES[@]}"; do
53+
example_path="$EXAMPLES_DIR/$example"
54+
55+
if [ ! -d "$example_path" ]; then
56+
echo "Warning: Example directory '$example_path' does not exist, skipping..."
57+
continue
58+
fi
59+
60+
echo "Building $example example..."
61+
cd "$example_path"
62+
63+
# Check if Cargo.toml exists
64+
if [ ! -f "Cargo.toml" ]; then
65+
echo "Warning: No Cargo.toml found in $example_path, skipping..."
66+
cd - > /dev/null
67+
continue
68+
fi
69+
70+
# Run the example
71+
if cargo run; then
72+
echo "✓ Successfully built $example"
73+
else
74+
echo "✗ Failed to build $example"
75+
exit 1
76+
fi
77+
78+
# Return to the original directory
79+
cd - > /dev/null
80+
done
81+
82+
echo "All examples built successfully!"

.github/workflows/book.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v4
18+
- name: Setup Chrome (for static_export)
19+
uses: browser-actions/setup-chrome@v1
20+
with:
21+
chrome-version: 'latest'
22+
install-chromedriver: true
1823
- run: cargo install mdbook --no-default-features --features search --vers "^0.4" --locked --quiet
1924
- name: Build examples
20-
run: |
21-
cd ${{ github.workspace }}/examples/basic_charts && cargo run
22-
cd ${{ github.workspace }}/examples/statistical_charts && cargo run
23-
cd ${{ github.workspace }}/examples/scientific_charts && cargo run
24-
cd ${{ github.workspace }}/examples/financial_charts && cargo run
25-
cd ${{ github.workspace }}/examples/3d_charts && cargo run
26-
cd ${{ github.workspace }}/examples/subplots && cargo run
27-
cd ${{ github.workspace }}/examples/shapes && cargo run
25+
run: .github/scripts/build-book-examples.sh ${{ github.workspace }}/examples
2826
- run: mdbook build docs/book
2927
- name: Checkout gh-pages branch
3028
run: |

.github/workflows/ci.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ jobs:
252252
targets: wasm32-unknown-unknown
253253
- run: cd ${{ github.workspace }}/examples/wasm-yew/${{ matrix.example }} && cargo build --target wasm32-unknown-unknown
254254

255-
256-
257255
build_book:
258256
name: Build Book
259257
runs-on: ubuntu-latest
@@ -270,15 +268,6 @@ jobs:
270268
- uses: dtolnay/rust-toolchain@stable
271269
- run: cargo install mdbook --no-default-features --features search --vers "^0.4" --locked --quiet
272270
- name: Build examples to generate needed html files
273-
run: |
274-
cd ${{ github.workspace }}/examples/basic_charts && cargo run
275-
cd ${{ github.workspace }}/examples/statistical_charts && cargo run
276-
cd ${{ github.workspace }}/examples/scientific_charts && cargo run
277-
cd ${{ github.workspace }}/examples/financial_charts && cargo run
278-
cd ${{ github.workspace }}/examples/3d_charts && cargo run
279-
cd ${{ github.workspace }}/examples/subplots && cargo run
280-
cd ${{ github.workspace }}/examples/shapes && cargo run
281-
cd ${{ github.workspace }}/examples/themes && cargo run
282-
cd ${{ github.workspace }}/examples/static_export && cargo run
271+
run: .github/scripts/build-book-examples.sh ${{ github.workspace }}/examples
283272
- name: Build book
284273
run: mdbook build docs/book
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Publish plotly-static
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
create-crates-io-release:
8+
name: Deploy to crates.io
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: dtolnay/rust-toolchain@stable
13+
- name: Setup Chrome (for static_export)
14+
uses: browser-actions/setup-chrome@v1
15+
with:
16+
chrome-version: 'latest'
17+
install-chromedriver: true
18+
19+
- run: cargo login ${{ env.CRATES_IO_TOKEN }}
20+
env:
21+
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
22+
- run: cargo publish --allow-dirty -p plotly_static --features webdriver_downlaod,chromedriver

.github/workflows/release.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ jobs:
1515
- run: cargo login ${{ env.CRATES_IO_TOKEN }}
1616
env:
1717
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
18+
- name: Setup Chrome (for static_export)
19+
uses: browser-actions/setup-chrome@v1
20+
with:
21+
chrome-version: 'latest'
22+
install-chromedriver: true
1823
- run: cargo publish --allow-dirty -p plotly_derive
1924
- run: sleep 10
2025
- run: cargo publish --allow-dirty -p plotly_kaleido
2126
- run: sleep 10
27+
- run: cargo publish --allow-dirty -p plotly_static --features webdriver_downlaod,chromedriver
28+
- run: sleep 10
2229
- run: cargo publish --allow-dirty -p plotly
2330

2431
create-gh-release:

docs/book/src/fundamentals/static_image_export.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ The static export functionality is controlled by feature flags in the main `plot
2121
### Cargo.toml Configuration Examples:
2222

2323
```toml
24-
# Basic usage with manual WebDriver installation
24+
# Basic usage with manual Chromedriver installation
2525
[dependencies]
2626
plotly = { version = "0.13", features = ["static_export_chromedriver"] }
2727

28-
# With automatic WebDriver download
28+
# With automatic Chromedriver download
2929
[dependencies]
3030
plotly = { version = "0.13", features = ["static_export_chromedriver", "static_export_wd_download"] }
3131

32-
# Recommended: Default configuration with Firefox + auto-download
32+
# Recommended: Default configuration with Chromedriver + auto-download
3333
[dependencies]
3434
plotly = { version = "0.13", features = ["static_export_default"] }
3535
```
@@ -43,9 +43,13 @@ plotly = { version = "0.13", features = ["static_export_default"] }
4343

4444
2. **Browser Installation**: You need Chrome/Chromium or Firefox installed
4545

46-
3. **Environment Variable** (optional): Set `WEBDRIVER_PATH` to specify custom WebDriver binary location (should point to the full executable path)
46+
3. **Environment Variables** (optional):
47+
- Set `WEBDRIVER_PATH` to specify custom WebDriver binary location (should point to the full executable path)
48+
- Set `BROWSER_PATH` to specify custom browser binary location (should point to the full executable path)
49+
4750
```bash
4851
export WEBDRIVER_PATH=/path/to/chromedriver
52+
export BROWSER_PATH=/path/to/chrome
4953
```
5054

5155
## Basic Usage

examples/custom_controls/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fn main() {
426426
bar_chart_with_modifiable_bar_mode(false, "bar_chart");
427427

428428
// Silder examples
429-
bar_chart_with_slider_customization(true, "bar_chart_with_slider_customization");
429+
bar_chart_with_slider_customization(false, "bar_chart_with_slider_customization");
430430
sinusoidal_slider_example(false, "sinusoidal_slider_example");
431431
gdp_life_expectancy_slider_example(false, "gdp_life_expectancy_slider_example");
432432
}

examples/kaleido/src/main.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ fn main() {
1717
// The image will be saved to format!("output/image.{image_format}") relative to
1818
// the current working directory.
1919
#[allow(deprecated)]
20-
plot.write_image(&filename, ImageFormat::EPS, width, height, scale);
21-
plot.write_image(&filename, ImageFormat::JPEG, width, height, scale);
22-
plot.write_image(&filename, ImageFormat::PDF, width, height, scale);
23-
plot.write_image(&filename, ImageFormat::PNG, width, height, scale);
24-
plot.write_image(&filename, ImageFormat::SVG, width, height, scale);
25-
plot.write_image(&filename, ImageFormat::WEBP, width, height, scale);
20+
{
21+
plot.write_image(&filename, ImageFormat::EPS, width, height, scale);
22+
plot.write_image(&filename, ImageFormat::JPEG, width, height, scale);
23+
plot.write_image(&filename, ImageFormat::PDF, width, height, scale);
24+
plot.write_image(&filename, ImageFormat::PNG, width, height, scale);
25+
plot.write_image(&filename, ImageFormat::SVG, width, height, scale);
26+
plot.write_image(&filename, ImageFormat::WEBP, width, height, scale);
2627

27-
let _svg_string = plot.to_svg(width, height, scale);
28+
let svg_string = plot.to_svg(width, height, scale);
29+
println!("SVG plot string: {svg_string}");
30+
}
2831
}

examples/static_export/README.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
# Static Export Example
22

3-
This example demonstrates how to use the `plotly_static` crate for exporting Plotly plots to static images using WebDriver and headless browsers. See also the `Static Image Export` section in the book for a more detailed description.
3+
This example demonstrates how to use the `plotly_static` crate for exporting Plotly plots to static images.
4+
5+
The `plotly_static` provides a interface for converting Plotly plots into various static image formats (PNG, JPEG, WEBP, SVG, PDF) using WebDriver and headless browsers.
6+
7+
In this example it is shown how to use the `StaticExporter` with the old style Kaleido API and also with the new style API. Using the former API is fine for one time static exports, but that API will crate an instance of the `StaticExporter` for each `write_image` call. The new style API is recommended for performance as the same instance of the `StaticExporter` can be reused accross mutliple exports.
8+
9+
See also the `Static Image Export` section in the book for a more detailed description.
410

511
## Overview
612

7-
The `plotly_static` crate provides a high-level interface for converting Plotly JSON plots into various static image formats (PNG, JPEG, WEBP, SVG, PDF) using WebDriver and headless browsers.
813

9-
## Features Demonstrated
14+
## Features
1015

1116
- **Multiple Export Formats**: PNG, JPEG, SVG, PDF
12-
- **Exporter Reuse**: Efficient reuse of a single `StaticExporter` instance
17+
- **Exporter Reuse (new API)**: Efficient reuse of a single `StaticExporter` instance
1318
- **String Export**: Base64 and SVG string output for web applications
1419
- **Logging**: Debug information and progress monitoring
15-
- **Error Handling**: Proper error handling with `Result` types
20+
- **Error Handling**: Error handling with `Result` types
1621

1722
## Prerequisites
1823

1924
1. **Browser Installation**: You need either Firefox (for geckodriver) or Chrome/Chromium (for chromedriver)
20-
2. **WebDriver**: Automatically downloaded with the `static_export_default` feature
21-
3. **Internet Connection**: Required for initial WebDriver download (if using `webdriver_download` feature)
25+
2. **WebDriver**: Chromedriver automatically downloaded with the `static_export_default` feature
26+
3. **Internet Connection**: Required for WebDriver download (if using `webdriver_download` feature)
2227

2328
## Feature Flags
2429

@@ -30,13 +35,13 @@ The example uses `static_export_default` which includes:
3035
### Alternative Configurations
3136

3237
```toml
33-
# Use Chrome/Chromium instead of Firefox
34-
plotly = { version = "0.13", features = ["static_export_chromedriver", "static_export_wd_download"] }
38+
# Use Firefox instead of Chrome/Chromium
39+
plotly = { version = "0.13", features = ["static_export_geckodriver", "static_export_wd_download"] }
3540

36-
# Manual WebDriver installation (no automatic download)
41+
# Manual Geckodriver installation (no automatic download)
3742
plotly = { version = "0.13", features = ["static_export_geckodriver"] }
3843

39-
# Chrome with manual installation
44+
# Manual Chromedriver installation (no automatic download)
4045
plotly = { version = "0.13", features = ["static_export_chromedriver"] }
4146
```
4247

@@ -50,7 +55,7 @@ cargo run
5055
RUST_LOG=debug cargo run
5156

5257
# With custom WebDriver path
53-
WEBDRIVER_PATH=/path/to/geckodriver cargo run
58+
WEBDRIVER_PATH=/path/to/chromedriver cargo run
5459
```
5560

5661
## Output
@@ -61,17 +66,11 @@ The example generates several files:
6166
- `plot3.svg`: Vector format, scalable, good for web
6267
- `plot1.pdf`: Vector format, good for printing
6368

64-
## Performance Tips
65-
66-
1. **Reuse Exporters**: Create a single `StaticExporter` and reuse it for multiple plots
67-
2. **Parallel Usage**: Use unique ports for parallel operations (tests, etc.)
68-
3. **Resource Management**: The exporter automatically manages WebDriver lifecycle
69-
7069
## Advanced Configuration
7170

7271
The example includes commented code showing advanced configuration options:
7372
- Custom WebDriver ports for parallel usage
74-
- Offline mode for bundled JavaScript
73+
- Offline mode for bundled JavaScript
7574
- Custom browser capabilities
7675
- Explicit WebDriver spawning
7776

@@ -81,11 +80,10 @@ The example includes commented code showing advanced configuration options:
8180

8281
1. **WebDriver not found**: Ensure the browser is installed and WebDriver is available
8382
2. **Port conflicts**: Use unique ports for parallel operations
84-
3. **Permission errors**: Ensure WebDriver has execute permissions
8583

8684
### Debug Information
8785

88-
Set `RUST_LOG=debug` to see detailed WebDriver operations and troubleshooting information.
86+
Set `RUST_LOG=debug` or`RUST_LOG=trace`to see detailed WebDriver operations and troubleshooting information.
8987

9088
## Related Documentation
9189

0 commit comments

Comments
 (0)