11import os
2+ from pathlib import Path
23import glob
34import logging
45
123124
124125.. figure:: /{thumbnail}
125126
126- :ref:`{ref_name } <{backend}gallery_{ref_name}>`
127+ :ref:`{label } <{backend}gallery_{ref_name}>`
127128
128129.. raw:: html
129130
203204}
204205
205206def generate_file_rst (app , src_dir , dest_dir , page , section , backend ,
206- img_extension , skip , deployment_url ):
207+ img_extension , skip , deployment_urls ):
207208 gallery_conf = app .config .nbsite_gallery_conf
208209 content = gallery_conf ['galleries' ][page ]
209210 host = gallery_conf ['host' ]
@@ -229,16 +230,11 @@ def generate_file_rst(app, src_dir, dest_dir, page, section, backend,
229230
230231 # Try to fetch all deployed examples
231232 deployed_examples = []
232- deployed_project = False
233233 if bs4 and endpoint is not None :
234234 r = requests .get (endpoint )
235235 if r .status_code == 200 :
236236 soup = bs4 .BeautifulSoup (r .content , features = 'lxml' )
237237 deployed_examples = [l .text for l in soup .find ('ul' ).find_all ('a' )]
238- if deployment_url is not None :
239- r = requests .get (deployment_url )
240- if r .status_code == 200 :
241- deployed_project = True
242238
243239 for f in files :
244240 extension = f .split ('.' )[- 1 ]
@@ -260,12 +256,25 @@ def generate_file_rst(app, src_dir, dest_dir, page, section, backend,
260256 prefix = '_' .join ([backend , 'gallery' ])
261257 else :
262258 prefix = 'gallery'
263- rst_file .write ('.. _%s_%s:\n \n ' % (prefix , basename [: - ( len ( extension ) + 1 )] ))
259+ rst_file .write ('.. _%s_%s:\n \n ' % (prefix , name ))
264260 rst_file .write (title + '\n ' )
265261 rst_file .write ('_' * len (title )+ '\n \n ' )
266262
263+ deployed_file = False
264+ for deployment_url in deployment_urls :
265+ if deployed_file :
266+ continue
267+ p = Path (deployment_url )
268+ if p .parts [- 1 ] == 'notebooks' :
269+ deployed_file = os .path .join (deployment_url , basename )
270+ else :
271+ deployed_file = os .path .join (deployment_url , name )
272+ r = requests .get (deployed_file )
273+ if r .status_code != 200 :
274+ deployed_file = False
275+
267276 if nblink in ['top' , 'both' ]:
268- add_nblink (rst_file , host , deployed_project , deployment_url , download_as ,
277+ add_nblink (rst_file , host , deployed_file , download_as ,
269278 org , proj , components , basename , ftype , section )
270279 rst_file .write ('\n \n -------\n \n ' )
271280
@@ -275,21 +284,21 @@ def generate_file_rst(app, src_dir, dest_dir, page, section, backend,
275284 rst_file .write ('\n :skip_execute: True\n ' )
276285 if deployed :
277286 rst_file .write (IFRAME_TEMPLATE .format (
278- background = iframe_spinner , url = endpoint + basename [: - 6 ] ))
287+ background = iframe_spinner , url = endpoint + name ))
279288 else :
280289 rst_file .write ('.. literalinclude:: %s\n \n ' % rel_path )
281- url = os .path .join ('thumbnails' , '%s.%s' % (basename [: - ( len ( extension ) + 1 )] , img_extension ))
290+ url = os .path .join ('thumbnails' , '%s.%s' % (name , img_extension ))
282291 rst_file .write ('.. figure:: %s\n \n ' % url )
283292
284293 if nblink in ['bottom' , 'both' ]:
285294 rst_file .write ('\n \n -------\n \n ' )
286- add_nblink (rst_file , host , deployed_project , deployment_url , download_as ,
295+ add_nblink (rst_file , host , deployed_file , download_as ,
287296 org , proj , components , basename , ftype , section )
288297
289- def add_nblink (rst_file , host , deployed_project , deployment_url , download_as ,
298+ def add_nblink (rst_file , host , deployed_file , download_as ,
290299 org , proj , components , basename , ftype , section ):
291- if deployed_project :
292- rst_file .write (f'`View a running version of this project . <{ deployment_url } >`_ | ' )
300+ if deployed_file :
301+ rst_file .write (f'`View a running version of this notebook . <{ deployed_file } >`_ | ' )
293302 if host == 'GitHub' and org and proj :
294303 rst_file .write ('`Download this {ftype} from GitHub (right-click to download).'
295304 ' <https://raw.githubusercontent.com/{org}/{proj}/master/{path}/{basename}>`_' .format (
@@ -381,7 +390,7 @@ def generate_gallery(app, page):
381390 description = section .get ('description' , None )
382391 labels = section .get ('labels' , [])
383392 subsection_order = section .get ('within_subsection_order' , sort_fn )
384- deployment_url = section .get ('deployment_url ' )
393+ deployment_urls = section .get ('deployment_urls ' )
385394 section = section ['path' ]
386395 else :
387396 heading = section .title ()
@@ -390,7 +399,7 @@ def generate_gallery(app, page):
390399 subsection_order = sort_fn
391400 description = None
392401 labels = []
393- deployment_url = None
402+ deployment_urls = []
394403
395404 if heading :
396405 gallery_rst += f'\n \n .. raw:: html\n \n <div class="section sphx-glr-section" id="{ section } -section"><h2>{ heading } </h2>\n \n '
@@ -517,7 +526,7 @@ def generate_gallery(app, page):
517526
518527 gallery_rst += this_entry
519528 generate_file_rst (app , path , dest_dir , page , section ,
520- backend , thumb_extension , skip , deployment_url )
529+ backend , thumb_extension , skip , deployment_urls )
521530 # clear at the end of the section
522531 gallery_rst += CLEAR_DIV
523532 gallery_rst += '\n \n .. raw:: html\n \n </div>\n \n '
0 commit comments