Skip to content

Commit e325acb

Browse files
author
Gordon Shotwell
committed
Check if asset files exist.
1 parent 2c3bd1f commit e325acb

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

shiny/examples/include_css/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import os
1+
from pathlib import Path
22

33
from shiny import *
44

5-
css_file = os.path.join(os.path.dirname(__file__), "css/styles.css")
5+
css_file = Path(__file__).parent / "css" / "styles.css"
66

77
app_ui = ui.page_fluid(
88
"Almost before we knew it, we had left the ground!!!",
9-
ui.include_css(css_file, method="link_files"),
9+
ui.include_css(css_file),
1010
ui.div(
1111
# Style individual elements with an attribute dictionary.
1212
{"style": "font-weight: bold"},

shiny/examples/include_javascript/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import os
1+
from pathlib import Path
22

33
from shiny import *
44

5-
js_file = os.path.join(os.path.dirname(__file__), "js/app.js")
5+
js_file = Path(__file__).parent / "js" / "app.js"
66

77
app_ui = ui.page_fluid(
88
"If you see this page before 'OK'-ing the alert box, something went wrong",

shiny/ui/_include_helpers.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ def include_js(
8282
~ui.tags.script
8383
~include_css
8484
"""
85+
file_path = check_path(path)
8586

8687
if method == "inline":
87-
return tags.script(read_utf8(path), **kwargs)
88+
return tags.script(read_utf8(file_path), **kwargs)
8889

8990
include_files = method == "link_files"
90-
path_dest, hash = maybe_copy_files(path, include_files)
91+
path_dest, hash = maybe_copy_files(file_path, include_files)
9192

9293
dep, src = create_include_dependency("include-js-" + hash, path_dest, include_files)
9394

@@ -159,11 +160,12 @@ def include_css(
159160
~include_js
160161
"""
161162

163+
file_path = check_path(path)
162164
if method == "inline":
163-
return tags.style(read_utf8(path), type="text/css")
165+
return tags.style(read_utf8(file_path), type="text/css")
164166

165167
include_files = method == "link_files"
166-
path_dest, hash = maybe_copy_files(path, include_files)
168+
path_dest, hash = maybe_copy_files(file_path, include_files)
167169

168170
dep, src = create_include_dependency(
169171
"include-css-" + hash, path_dest, include_files
@@ -177,6 +179,17 @@ def include_css(
177179
# ---------------------------------------------------------------------------
178180

179181

182+
def check_path(path: Path | str) -> Path:
183+
path = Path(path)
184+
if not path.exists():
185+
err = f"""
186+
{path.absolute()} does not exist.
187+
Files are typically placed in the app directory and refered to with 'Path(__file__) / {path.name}'
188+
"""
189+
raise RuntimeError(err)
190+
return path
191+
192+
180193
def create_include_dependency(
181194
name: str, path: str, include_files: bool
182195
) -> tuple[HTMLDependency, str]:
@@ -232,8 +245,8 @@ def get_hash(path: Path | str, include_files: bool) -> str:
232245

233246

234247
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))
248+
path = Path(path)
249+
return str(path) + "-" + str(path.stat().st_mtime)
237250

238251

239252
def hash_deterministic(s: str) -> str:

0 commit comments

Comments
 (0)