77import os
88import shutil
99import tempfile
10+ from pathlib import Path
1011
1112# TODO: maybe these include_*() functions should actually live in htmltools?
1213from htmltools import HTMLDependency , Tag , TagAttrValue , tags
2021
2122@add_example ()
2223def include_js (
23- path : str ,
24+ path : Path | str ,
2425 * ,
2526 method : Literal ["link" , "link_files" , "inline" ] = "link" ,
2627 ** kwargs : TagAttrValue ,
@@ -67,14 +68,14 @@ def include_js(
6768
6869 .. code-block:: python
6970
70- ui.fluidPage(
71- head_content(ui.include_js("custom.js")),
72- )
71+ ui.fluidPage(
72+ head_content(ui.include_js("custom.js")),
73+ )
7374
74- # Alternately you can inline Javscript by changing the method.
75- ui.fluidPage(
76- head_content(ui.include_js("custom.js", method = "inline")),
77- )
75+ # Alternately you can inline Javscript by changing the method.
76+ ui.fluidPage(
77+ head_content(ui.include_js("custom.js", method = "inline")),
78+ )
7879
7980 See Also
8081 --------
@@ -83,7 +84,7 @@ def include_js(
8384 """
8485
8586 if method == "inline" :
86- return tags .script (read_utf8 (path ), type = "text/javascript" , ** kwargs )
87+ return tags .script (read_utf8 (path ), ** kwargs )
8788
8889 include_files = method == "link_files"
8990 path_dest , hash = maybe_copy_files (path , include_files )
@@ -122,10 +123,13 @@ def include_css(
122123 * ``"inline"``: Inline the CSS file contents within a :func:`~ui.tags.style`
123124 tag.
124125
126+
125127 Returns
126128 -------
127- If ``method="inline"``, returns a :func:`~ui.tags.style` tag; otherwise, returns a
128- :func:`~ui.tags.link` tag.
129+ :
130+
131+ If ``method="inline"``, returns a :func:`~ui.tags.style` tag; otherwise, returns a
132+ :func:`~ui.tags.link` tag.
129133
130134 Note
131135 ----
@@ -136,17 +140,17 @@ def include_css(
136140
137141 .. code-block:: python
138142
139- from htmltools import head_content from shiny import ui
143+ from htmltools import head_content from shiny import ui
140144
141- ui.fluidPage(
142- head_content(ui.include_css("custom.css")),
145+ ui.fluidPage(
146+ head_content(ui.include_css("custom.css")),
143147
144- # You can also inline css by passing a dictionary with a `style` element.
145- ui.div(
146- {"style": "font-weight: bold;"},
147- ui.p("Some text!"),
148+ # You can also inline css by passing a dictionary with a `style` element.
149+ ui.div(
150+ {"style": "font-weight: bold;"},
151+ ui.p("Some text!"),
152+ )
148153 )
149- )
150154
151155 See Also
152156 --------
@@ -191,7 +195,7 @@ def create_include_dependency(
191195 return dep , src
192196
193197
194- def maybe_copy_files (path : str , include_files : bool ) -> tuple [str , str ]:
198+ def maybe_copy_files (path : Path | str , include_files : bool ) -> tuple [str , str ]:
195199 hash = get_hash (path , include_files )
196200
197201 # To avoid unnecessary work when the same file is included multiple times,
@@ -217,7 +221,7 @@ def maybe_copy_files(path: str, include_files: bool) -> tuple[str, str]:
217221 return path_dest , hash
218222
219223
220- def get_hash (path : str , include_files : bool ) -> str :
224+ def get_hash (path : Path | str , include_files : bool ) -> str :
221225 if include_files :
222226 key = get_file_key (path )
223227 else :
@@ -227,8 +231,9 @@ def get_hash(path: str, include_files: bool) -> str:
227231 return hash_deterministic (key )
228232
229233
230- def get_file_key (path : str ) -> str :
231- return path + "-" + str (os .path .getmtime (path ))
234+ def get_file_key (path : Path | str ) -> str :
235+ path_str = str (path ) if isinstance (path , Path ) else path
236+ return path_str + "-" + str (os .path .getmtime (path_str ))
232237
233238
234239def hash_deterministic (s : str ) -> str :
@@ -238,7 +243,7 @@ def hash_deterministic(s: str) -> str:
238243 return hashlib .sha1 (s .encode ("utf-8" )).hexdigest ()
239244
240245
241- def read_utf8 (path : str ) -> str :
246+ def read_utf8 (path : Path | str ) -> str :
242247 with open (path , "r" , encoding = "utf-8" ) as f :
243248 return f .read ()
244249
0 commit comments