Skip to content

Commit 14012c8

Browse files
committed
Added comments, minor changes
1 parent c1d7568 commit 14012c8

File tree

10 files changed

+79
-26
lines changed

10 files changed

+79
-26
lines changed

shiny/ui/_include_helpers.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -226,50 +226,45 @@ def maybe_copy_files(path: Path | str, include_files: bool) -> tuple[str, str]:
226226
# Since the hash/tmpdir should represent all the files in the path's directory,
227227
# we can check if it exists to determine if we have a cache hit
228228
if os.path.exists(path_dest):
229-
print("Already exists", path_dest)
229+
print("Path already exists:", path_dest)
230230
return path_dest, hash
231231

232-
permissions = (
233-
0o755 # Store permissions var set to -rwxr-xr-x (owner rwx, others rx)
234-
)
235-
236232
# Otherwise, make sure we have a clean slate
237233
if os.path.exists(tmpdir):
238-
print("folder exists, not contents:", os.stat(tmpdir))
234+
print("Folder already exists, but not files, removing.")
239235
shutil.rmtree(tmpdir)
240236

241-
# Looks like this is copying twice? puts stuff in diff folders?
237+
# This recursively changes permissions to 755 (owner rwx, other rx) for all files
238+
# under `tmp/unique_hash` so that the app can be run by other collaborators
239+
# on a multi-tenant system. See #2061
242240
if include_files:
243241
shutil.copytree(os.path.dirname(path), tmpdir)
244-
print("new folder, perms: ", oct(os.stat(tmpdir).st_mode))
245-
for root, dirs, files in os.walk(tmpdir):
246-
# set perms on sub-directories
247-
for dir in dirs:
248-
os.chmod(os.path.join(root, dir), permissions)
249-
print("Dir: ", dir)
250-
print("dir perms: ", oct(os.stat(os.path.join(root, dir)).st_mode))
251-
242+
for dirpath, dirs, filenames in os.walk(tmpdir):
252243
# set perms on files
253-
for file in files:
254-
print("File: ", file)
255-
os.chmod(os.path.join(root, file), permissions)
256-
print("file perms: ", oct(os.stat(os.path.join(root, file)).st_mode))
257-
244+
for file in filenames:
245+
os.chmod(os.path.join(dirpath, file), 0o755)
246+
print(
247+
"File: ",
248+
file,
249+
"File perms: ",
250+
oct(os.stat(os.path.join(dirpath, file)).st_mode),
251+
)
252+
else:
253+
os.mkdir(tmpdir, mode=0o755)
258254
shutil.copy(path, path_dest)
259-
print("PATH_DEST: ", path_dest)
255+
os.chmod(path_dest, 0o755)
256+
print("File: ", path_dest, "perms", oct(os.stat(path_dest).st_mode))
257+
260258
return path_dest, hash
261259

262260

263261
def get_hash(path: Path | str, include_files: bool) -> str:
264262
if include_files:
265-
print("get_hash, include_files is true")
266263
dir = os.path.dirname(path)
267264
files = glob.iglob(os.path.join(dir, "**"), recursive=True)
268265
key = "\n".join([get_file_key(x) for x in files])
269-
print("KEY: ", key)
270266
else:
271267
key = get_file_key(path)
272-
print("not include key: ", key)
273268

274269
return hash_deterministic(key)
275270

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@import url("./evenmorecss/more.css");
2+
3+
body {
4+
font-family: Arial, sans-serif;
5+
}
6+
7+
h1 {
8+
color: black;
9+
border-bottom: 2px solid #4682b4;
10+
padding-bottom: 10px;
11+
}
12+
13+
.content {
14+
margin: 20px;
15+
padding: 15px;
16+
background-color: white;
17+
border-radius: 5px;
18+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pathlib import Path
2+
3+
from shiny import App, Inputs, Outputs, Session, ui
4+
5+
js_file = Path(__file__).parent / "customjs.js"
6+
css_file = Path(__file__).parent / "style.css"
7+
8+
# Define the UI
9+
app_ui = ui.page_fluid(
10+
ui.include_css(css_file, method="link"),
11+
ui.include_js(js_file, method="inline"),
12+
ui.h1("Simple Shiny App with External CSS"),
13+
ui.div(
14+
ui.p("This is a simple Shiny app that demonstrates ui.include_css()"),
15+
ui.p("The styling comes from an external CSS file!"),
16+
class_="content",
17+
),
18+
)
19+
20+
21+
# Define the server
22+
def server(input: Inputs, output: Outputs, session: Session):
23+
pass
24+
25+
26+
# Create and run the app
27+
app = App(app_ui, server)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const newParagraph = document.createElement('p');
2+
newParagraph.textContent = 'Heyo!';
3+
const bodyElement = document.body;
4+
bodyElement.appendChild(newParagraph);

tests/playwright/shiny/bugs/2061-css-js-inclusion/css/style.css renamed to tests/playwright/shiny/bugs/2061-css-js-inclusion/not_include_files_case/style.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
@import url("more.css");
2-
31
body {
2+
background-color: #70bfef;
43
font-family: Arial, sans-serif;
54
}
65

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from playwright.sync_api import Page, expect
2+
3+
from shiny.run import ShinyAppProc
4+
5+
6+
def test_inclusion(page: Page, local_app: ShinyAppProc) -> None:
7+
page.goto(local_app.url)
8+
9+
expect(page.locator("body > p")).to_have_text("Heyo!")

0 commit comments

Comments
 (0)