5050from .downsample import downsample
5151from .utils import read_package_file
5252
53- DATATABLES_SRC_FOR_ITABLES = (
54- f"_datatables_src_for_itables_ { itables_version .replace ('.' ,'_' ).replace ('-' ,'_' )} "
53+ _ITABLES_UNDERSCORE_VERSION = (
54+ f"_itables_ { itables_version .replace ('.' ,'_' ).replace ('-' ,'_' )} "
5555)
56+ _ITABLES_READY_EVENT = f"itables-{ itables_version } -ready"
5657_OPTIONS_NOT_AVAILABLE_IN_APP_MODE = {
5758 "connected" ,
5859 "dt_url" ,
@@ -179,7 +180,7 @@ def init_notebook_mode(
179180 )
180181 local_import = (
181182 "const { set_or_remove_dark_class } = await import(window."
182- + DATATABLES_SRC_FOR_ITABLES
183+ + _ITABLES_UNDERSCORE_VERSION
183184 + ");"
184185 )
185186 init_datatables = replace_value (init_datatables , connected_import , local_import )
@@ -201,20 +202,34 @@ def generate_init_offline_itables_html(dt_bundle: Union[Path, str]) -> str:
201202 assert dt_bundle .suffix == ".js"
202203 dt_src = dt_bundle .read_text (encoding = "utf-8" )
203204 dt_css = dt_bundle .with_suffix (".css" ).read_text (encoding = "utf-8" )
204- dt64 = b64encode (dt_src .encode ("utf-8" )).decode ("ascii" )
205+ dt_src_b64 = b64encode (dt_src .encode ("utf-8" )).decode ("ascii" )
206+ dt_css_b64 = b64encode (dt_css .encode ("utf-8" )).decode ("ascii" )
207+
208+ init_notebook_mode = read_package_file ("html/init_notebook_offline.html" )
209+ init_notebook_mode = replace_value (
210+ init_notebook_mode ,
211+ "_itables_underscore_version" ,
212+ _ITABLES_UNDERSCORE_VERSION ,
213+ expected_count = 3 ,
214+ )
215+ init_notebook_mode = replace_value (
216+ init_notebook_mode , "itables-version-ready" , _ITABLES_READY_EVENT
217+ )
218+ init_notebook_mode = replace_value (init_notebook_mode , "dt_src_b64" , dt_src_b64 )
219+ init_notebook_mode = replace_value (init_notebook_mode , "dt_css_b64" , dt_css_b64 )
205220
206- return f"""<style>{ dt_css } </style>
221+ return (
222+ init_notebook_mode
223+ + f"""
207224<div style="vertical-align:middle; text-align:left">
208- <script>
209- window.{ DATATABLES_SRC_FOR_ITABLES } = "data:text/javascript;base64,{ dt64 } ";
210- </script>
211225<noscript>
212226{ get_animated_logo (opt .display_logo_when_loading )}
213227This is the <code>init_notebook_mode</code> cell from ITables v{ itables_version } <br>
214228(you should not see this message - is your notebook <it>trusted</it>?)
215229</noscript>
216230</div>
217231"""
232+ )
218233
219234
220235def _table_header (
@@ -297,17 +312,15 @@ def get_keys_to_be_evaluated(data: Any) -> list[list[Union[int, str]]]:
297312 return keys_to_be_evaluated
298313
299314
300- def replace_value (template : str , pattern : str , value : str ) -> str :
315+ def replace_value (
316+ template : str , pattern : str , value : str , expected_count : int = 1
317+ ) -> str :
301318 """Set the given pattern to the desired value in the template,
302319 after making sure that the pattern is found exactly once."""
303320 count = template .count (pattern )
304- if not count :
305- raise ValueError ("pattern={} was not found in template" .format (pattern ))
306- elif count > 1 :
321+ if count != expected_count :
307322 raise ValueError (
308- "pattern={} was found multiple times ({}) in template" .format (
309- pattern , count
310- )
323+ f"{ pattern = } was found { count } times in template, expected { expected_count } ."
311324 )
312325 return template .replace (pattern , value )
313326
@@ -702,8 +715,8 @@ def html_table_from_template(
702715 )
703716
704717 # Load the HTML template
705- output = read_package_file ("html/datatables_template.html" )
706718 if connected :
719+ output = read_package_file ("html/datatables_template.html" )
707720 assert dt_url .endswith (".js" )
708721 output = replace_value (output , UNPKG_DT_BUNDLE_URL_NO_VERSION , dt_url )
709722 output = replace_value (
@@ -712,21 +725,14 @@ def html_table_from_template(
712725 dt_url [:- 3 ] + ".css" ,
713726 )
714727 else :
715- connected_style = (
716- f'<link href="{ UNPKG_DT_BUNDLE_CSS_NO_VERSION } " rel="stylesheet">\n '
717- )
718- output = replace_value (output , connected_style , "" )
719- connected_import = (
720- "import { ITable, jQuery as $ } from '"
721- + UNPKG_DT_BUNDLE_URL_NO_VERSION
722- + "';"
723- )
724- local_import = (
725- "const { ITable, jQuery: $ } = await import(window."
726- + DATATABLES_SRC_FOR_ITABLES
727- + ");"
728+ output = read_package_file ("html/datatables_template_offline.html" )
729+ output = replace_value (
730+ output ,
731+ "_itables_underscore_version" ,
732+ _ITABLES_UNDERSCORE_VERSION ,
733+ expected_count = 2 ,
728734 )
729- output = replace_value (output , connected_import , local_import )
735+ output = replace_value (output , "itables-version-ready" , _ITABLES_READY_EVENT )
730736
731737 itables_source = (
732738 "the internet" if connected else "the <code>init_notebook_mode</code> cell"
0 commit comments