44import tempfile
55from warnings import warn
66import glob
7+ import re
78
89from pathlib import Path
910
@@ -51,6 +52,7 @@ def __init__(
5152 height = "100%" ,
5253 prompt = False ,
5354 prompt_color = None ,
55+ search_params = [],
5456 ** attributes ,
5557 ):
5658 super ().__init__ (
@@ -60,10 +62,12 @@ def __init__(
6062 height = height ,
6163 prompt = prompt ,
6264 prompt_color = prompt_color ,
65+ search_params = search_params ,
6366 )
6467
6568 def html (self ):
6669 iframe_src = self ["iframe_src" ]
70+ search_params = self ["search_params" ]
6771
6872 if self ["prompt" ]:
6973 prompt = (
@@ -76,13 +80,24 @@ def html(self):
7680 placeholder_id = uuid4 ()
7781 container_style = f'width: { self ["width" ]} ; height: { self ["height" ]} ;'
7882
79- return (
80- f"<div class=\" jupyterlite_sphinx_iframe_container\" style=\" { container_style } \" onclick=window.jupyterliteShowIframe('{ placeholder_id } ','{ iframe_src } ')>"
81- f' <div id={ placeholder_id } class="jupyterlite_sphinx_try_it_button jupyterlite_sphinx_try_it_button_unclicked" style="background-color: { prompt_color } ;">'
82- f" { prompt } "
83- " </div>"
84- "</div>"
85- )
83+ return f"""
84+ <div
85+ class=\" jupyterlite_sphinx_iframe_container\"
86+ style=\" { container_style } \"
87+ onclick=\" window.jupyterliteShowIframe(
88+ '{ placeholder_id } ',
89+ window.jupyterliteConcatSearchParams('{ iframe_src } ', { search_params } )
90+ )\"
91+ >
92+ <div
93+ id={ placeholder_id }
94+ class=\" jupyterlite_sphinx_try_it_button jupyterlite_sphinx_try_it_button_unclicked\"
95+ style=\" background-color: { prompt_color } ;\"
96+ >
97+ { prompt }
98+ </div>
99+ </div>
100+ """
86101
87102 return (
88103 f'<iframe src="{ iframe_src } "'
@@ -196,6 +211,7 @@ class RepliteDirective(SphinxDirective):
196211 "theme" : directives .unchanged ,
197212 "prompt" : directives .unchanged ,
198213 "prompt_color" : directives .unchanged ,
214+ "search_params" : directives .unchanged ,
199215 }
200216
201217 def run (self ):
@@ -205,6 +221,8 @@ def run(self):
205221 prompt = self .options .pop ("prompt" , False )
206222 prompt_color = self .options .pop ("prompt_color" , None )
207223
224+ search_params = search_params_parser (self .options .pop ("search_params" , "" ))
225+
208226 prefix = os .path .relpath (
209227 os .path .join (self .env .app .srcdir , JUPYTERLITE_DIR ),
210228 os .path .dirname (self .get_source_info ()[0 ]),
@@ -218,6 +236,7 @@ def run(self):
218236 prompt = prompt ,
219237 prompt_color = prompt_color ,
220238 content = self .content ,
239+ search_params = search_params ,
221240 lite_options = self .options ,
222241 )
223242 ]
@@ -233,6 +252,7 @@ class _LiteDirective(SphinxDirective):
233252 "theme" : directives .unchanged ,
234253 "prompt" : directives .unchanged ,
235254 "prompt_color" : directives .unchanged ,
255+ "search_params" : directives .unchanged ,
236256 }
237257
238258 def run (self ):
@@ -242,6 +262,8 @@ def run(self):
242262 prompt = self .options .pop ("prompt" , False )
243263 prompt_color = self .options .pop ("prompt_color" , None )
244264
265+ search_params = search_params_parser (self .options .pop ("search_params" , "" ))
266+
245267 source_location = os .path .dirname (self .get_source_info ()[0 ])
246268
247269 prefix = os .path .relpath (
@@ -276,6 +298,7 @@ def run(self):
276298 height = height ,
277299 prompt = prompt ,
278300 prompt_color = prompt_color ,
301+ search_params = search_params ,
279302 lite_options = self .options ,
280303 )
281304 ]
@@ -476,3 +499,18 @@ def setup(app):
476499 app .add_css_file ("jupyterlite_sphinx.css" )
477500
478501 app .add_js_file ("jupyterlite_sphinx.js" )
502+
503+
504+ def search_params_parser (search_params : str ) -> str :
505+ pattern = re .compile (r"^\[(?:\s*[\"']{1}([^=\s\,&=\?\/]+)[\"']{1}\s*\,?)+\]$" )
506+ if not search_params :
507+ return ""
508+ if search_params in ["True" , "False" ]:
509+ return search_params .lower ()
510+ elif pattern .match (search_params ):
511+ return search_params .replace ('"' , "'" )
512+ else :
513+ raise ValueError (
514+ 'The search_params directive must be either True, False or ["param1", "param2"].\n '
515+ 'The params name shouldn\' t contain any of the following characters ["\\ ", "\' ", """, ",", "?", "=", "&", " ").'
516+ )
0 commit comments