Skip to content

Commit 5d6246b

Browse files
committed
expose plotly.io.get_chrome()
1 parent 6d864c4 commit 5d6246b

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

plotly/io/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ._html import to_html, write_html
1818
from ._renderers import renderers, show
1919
from . import base_renderers
20-
from ._kaleido import defaults
20+
from ._kaleido import defaults, get_chrome
2121

2222
__all__ = [
2323
"to_image",
@@ -38,6 +38,7 @@
3838
"base_renderers",
3939
"full_figure_for_development",
4040
"defaults",
41+
"get_chrome",
4142
]
4243
else:
4344
__all__, __getattr__, __dir__ = relative_import(
@@ -59,6 +60,7 @@
5960
"._renderers.renderers",
6061
"._renderers.show",
6162
"._kaleido.defaults",
63+
"._kaleido.get_chrome",
6264
],
6365
)
6466

plotly/io/_kaleido.py

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,13 @@ def full_figure_for_development(
775775
return go.Figure(fig, skip_invalid=True)
776776

777777

778-
def get_chrome() -> None:
778+
def plotly_get_chrome() -> None:
779779
"""
780780
Install Google Chrome for Kaleido (Required for Plotly image export).
781-
This function can be run from the command line using the command `plotly_get_chrome`
782-
defined in pyproject.toml
781+
This function is a command-line wrapper for `plotly.io.get_chrome()`.
782+
783+
When running from the command line, use the command `plotly_get_chrome`;
784+
when calling from Python code, use `plotly.io.get_chrome()`.
783785
"""
784786

785787
usage = """
@@ -813,16 +815,60 @@ def get_chrome() -> None:
813815

814816
# Handle "--path" flag
815817
chrome_install_path = None
816-
user_specified_path = False
817818
if "--path" in cli_args:
818819
path_index = cli_args.index("--path") + 1
819820
if path_index < len(cli_args):
820821
chrome_install_path = cli_args[path_index]
821822
cli_args.remove("--path")
822823
cli_args.remove(chrome_install_path)
823824
chrome_install_path = Path(chrome_install_path)
824-
user_specified_path = True
825+
826+
# If any arguments remain, command syntax was incorrect -- print usage and exit
827+
if len(cli_args) > 1:
828+
print(usage)
829+
sys.exit(1)
830+
831+
if not cli_yes:
832+
print(
833+
f"""
834+
Plotly will install a copy of Google Chrome to be used for generating static images of plots.
835+
Chrome will be installed at: {chrome_install_path}"""
836+
)
837+
response = input("Do you want to proceed? [y/n] ")
838+
if not response or response[0].lower() != "y":
839+
print("Cancelled")
840+
return
841+
print("Installing Chrome for Plotly...")
842+
exe_path = get_chrome(chrome_install_path)
843+
print("Chrome installed successfully.")
844+
print(f"The Chrome executable is now located at: {exe_path}")
845+
846+
847+
def get_chrome(path: Union[str, Path, None] = None) -> Path:
848+
"""
849+
Get the path to the Chrome executable for Kaleido.
850+
This function is used by the `plotly_get_chrome` command line utility.
851+
852+
Parameters
853+
----------
854+
path: str or Path or None
855+
The path to the directory where Chrome should be installed.
856+
If None, the default download path will be used.
857+
"""
858+
if not kaleido_available() or kaleido_major() < 1:
859+
raise ValueError(
860+
"""
861+
This command requires Kaleido v1.0.0 or greater.
862+
Install it using `pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`."
863+
"""
864+
)
865+
866+
# Use default download path if no path was specified
867+
if path:
868+
user_specified_path = True
869+
chrome_install_path = Path(path) # Ensure it's a Path object
825870
else:
871+
user_specified_path = False
826872
from choreographer.cli.defaults import default_download_path
827873

828874
chrome_install_path = default_download_path
@@ -848,25 +894,7 @@ def get_chrome() -> None:
848894
"""
849895
)
850896

851-
# If any arguments remain, command syntax was incorrect -- print usage and exit
852-
if len(cli_args) > 1:
853-
print(usage)
854-
sys.exit(1)
855-
856-
if not cli_yes:
857-
print(
858-
f"""
859-
Plotly will install a copy of Google Chrome to be used for generating static images of plots.
860-
Chrome will be installed at: {chrome_install_path}"""
861-
)
862-
response = input("Do you want to proceed? [y/n] ")
863-
if not response or response[0].lower() != "y":
864-
print("Cancelled")
865-
return
866-
print("Installing Chrome for Plotly...")
867-
exe_path = kaleido.get_chrome_sync(path=chrome_install_path)
868-
print("Chrome installed successfully.")
869-
print(f"The Chrome executable is now located at: {exe_path}")
897+
return kaleido.get_chrome_sync(path=chrome_install_path)
870898

871899

872900
__all__ = ["to_image", "write_image", "scope", "full_figure_for_development"]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ dev = [
8383
]
8484

8585
[project.scripts]
86-
plotly_get_chrome = "plotly.io._kaleido:get_chrome"
86+
plotly_get_chrome = "plotly.io._kaleido:plotly_get_chrome"
8787

8888
[tool.pytest.ini_options]
8989
markers = [

0 commit comments

Comments
 (0)