File tree Expand file tree Collapse file tree 2 files changed +54
-10
lines changed Expand file tree Collapse file tree 2 files changed +54
-10
lines changed Original file line number Diff line number Diff line change @@ -168,16 +168,28 @@ def to_image(
168168 $ pip install kaleido==0.2.1
169169"""
170170 )
171- img_bytes = kaleido .calc_fig_sync (
172- fig_dict ,
173- path = None ,
174- opts = dict (
175- format = format ,
176- width = width ,
177- height = height ,
178- scale = scale ,
179- ),
180- )
171+ import choreographer
172+
173+ try :
174+ img_bytes = kaleido .calc_fig_sync (
175+ fig_dict ,
176+ path = None ,
177+ opts = dict (
178+ format = format ,
179+ width = width ,
180+ height = height ,
181+ scale = scale ,
182+ ),
183+ )
184+ except choreographer .errors .ChromeNotFoundError :
185+ raise RuntimeError (
186+ """
187+
188+ Kaleido requires Google Chrome to be installed. Install it by running:
189+ $ plotly_install_chrome
190+ """
191+ )
192+
181193 else :
182194 # Kaleido v0
183195 warnings .warn (
@@ -192,6 +204,35 @@ def to_image(
192204 return img_bytes
193205
194206
207+ def install_chrome ():
208+ """
209+ Install Google Chrome for Kaleido
210+ This function can be run from the command line using the command plotly_install_chrome
211+ defined in pyproject.toml
212+ """
213+ if not kaleido_available or kaleido_major < 1 :
214+ raise ValueError (
215+ "This command requires Kaleido v1.0.0 or greater. Install it using `pip install kaleido`."
216+ )
217+ import choreographer
218+ import sys
219+
220+ cli_yes = len (sys .argv ) > 1 and sys .argv [1 ] == "-y"
221+ if not cli_yes :
222+ print (
223+ "\n Plotly will install a copy of Google Chrome to be used for generating static images of plots.\n "
224+ )
225+ # TODO: Print path where Chrome will be installed
226+ # print(f"Chrome will be installed at {chrome_download_path}\n")
227+ response = input ("Do you want to proceed? [y/n] " )
228+ if not response or response [0 ].lower () != "y" :
229+ print ("Cancelled" )
230+ return
231+ print ("Installing Chrome for Plotly..." )
232+ kaleido .get_chrome_sync ()
233+ print ("Chrome installed successfully." )
234+
235+
195236def write_image (
196237 fig ,
197238 file ,
Original file line number Diff line number Diff line change @@ -46,6 +46,9 @@ dependencies = [
4646[project .optional-dependencies ]
4747express = [" numpy" ]
4848
49+ [project .scripts ]
50+ plotly_install_chrome = " plotly.io._kaleido:install_chrome"
51+
4952[tool .setuptools .packages .find ]
5053where = [" ." ]
5154include = [" plotly*" , " _plotly*" ]
You can’t perform that action at this time.
0 commit comments