|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.8" |
| 3 | +# dependencies = [ |
| 4 | +# "pandas", |
| 5 | +# "plotly[express]", |
| 6 | +# "kaleido @ file:///${PROJECT_ROOT}/", |
| 7 | +# ] |
| 8 | +# /// |
| 9 | +"""Runs the examples in the documentation. Use `mkdir output/; uv run`.""" |
| 10 | + |
| 11 | +import asyncio |
| 12 | + |
| 13 | +import plotly.express as px |
| 14 | + |
| 15 | +import kaleido |
| 16 | + |
| 17 | +### SAMPLE DATA ### |
| 18 | + |
| 19 | +fig = px.scatter( |
| 20 | + px.data.iris(), |
| 21 | + x="sepal_length", |
| 22 | + y="sepal_width", |
| 23 | + color="species", |
| 24 | +) |
| 25 | + |
| 26 | +fig2 = px.line( |
| 27 | + px.data.gapminder().query("country=='Canada'"), |
| 28 | + x="year", |
| 29 | + y="lifeExp", |
| 30 | + title="Life expectancy in Canada", |
| 31 | +) |
| 32 | + |
| 33 | +figures = [fig, fig2] |
| 34 | + |
| 35 | +### WRITE FIGURES ### |
| 36 | + |
| 37 | +# Simple one image synchronous write |
| 38 | + |
| 39 | +kaleido.write_fig_sync(fig, path="./output/") |
| 40 | + |
| 41 | + |
| 42 | +# Multiple image write with error collection |
| 43 | + |
| 44 | +error_log = [] |
| 45 | + |
| 46 | +kaleido.write_fig_sync( |
| 47 | + figures, |
| 48 | + path="./output/", |
| 49 | + opts={"format": "jpg"}, |
| 50 | + error_log=error_log, |
| 51 | +) |
| 52 | + |
| 53 | +# Dump the error_log |
| 54 | + |
| 55 | +if error_log: |
| 56 | + for e in error_log: |
| 57 | + print(str(e)) # noqa: T201 |
| 58 | + raise RuntimeError("{len(error_log)} images failed.") |
| 59 | + |
| 60 | + |
| 61 | +# async/await style of above |
| 62 | + |
| 63 | +asyncio.run( |
| 64 | + kaleido.write_fig( |
| 65 | + figures, |
| 66 | + path="./output/", |
| 67 | + opts={"format": "jpg"}, |
| 68 | + error_log=error_log, |
| 69 | + ), |
| 70 | +) |
| 71 | + |
| 72 | +### Make a figure generator |
| 73 | + |
| 74 | + |
| 75 | +def generate_figures(): # can be async as well |
| 76 | + """Generate plotly figures for each country in gapminder.""" |
| 77 | + data = px.data.gapminder() |
| 78 | + for country in data["country"].unique(): # list all countries in dataset |
| 79 | + # yield unique plot for each country |
| 80 | + yield px.line( |
| 81 | + data.query(f'country=="{country}"'), |
| 82 | + x="year", |
| 83 | + y="lifeExp", |
| 84 | + title=f"Life expectancy in {country}", |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +kaleido.write_fig_sync(generate_figures(), path="./output/", n=15) |
| 89 | +# file names will be taken from figure title |
| 90 | + |
| 91 | + |
| 92 | +### If you need more control, use an object |
| 93 | + |
| 94 | + |
| 95 | +def generate_figure_objects(): |
| 96 | + """Generate plotly figure objects for each country in gapminder.""" |
| 97 | + data = px.data.gapminder() |
| 98 | + for country in data["country"].unique(): # list all countries in dataset |
| 99 | + fig = px.line( |
| 100 | + data.query(f'country=="{country}"'), |
| 101 | + x="year", |
| 102 | + y="lifeExp", |
| 103 | + title=f"Life expectancy in {country}", |
| 104 | + ) |
| 105 | + yield {"fig": fig, "path": f"./output/{country}.jpg"} |
| 106 | + # customize file name |
| 107 | + |
| 108 | + |
| 109 | +# use 15 processes |
| 110 | +kaleido.write_fig_from_object_sync(generate_figure_objects(), n=15) |
0 commit comments