Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include helpers don't work when used twice in the same dir #729

Open
jcheng5 opened this issue Sep 6, 2023 · 3 comments
Open

include helpers don't work when used twice in the same dir #729

jcheng5 opened this issue Sep 6, 2023 · 3 comments

Comments

@jcheng5
Copy link
Collaborator

jcheng5 commented Sep 6, 2023

From Discord thread: https://discord.com/channels/1109483223987277844/1148651437820231710

Basically, if you use two (or more?) of the include helpers (ui.include_js, ui.include_css, etc.) pointing to files in the same directory, only one of them actually succeeds. The others 404.

shinylive repro link

@pieterjanvc
Copy link

pieterjanvc commented May 9, 2024

Hello,

Any updates on this? I'm facing the same issue and how it can be fixed soon. (the workaround is good for now though, but it took me ages to find this post and spent debugging in vain)

Thanks!

@rkaramc
Copy link

rkaramc commented Sep 24, 2024

Hi,
Problem occurs because maybe_copy_files(path, include_files) is dropping the tmpdir before copying the file at path to it.

def maybe_copy_files(path: Path | str, include_files: bool) -> tuple[str, str]:
hash = get_hash(path, include_files)

# Otherwise, make sure we have a clean slate
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)

Related issue at #1427 (comment)_. @Konkrad fixed it by placing each file in its own directory.

I fixed the issue by placing the files all in a single directory, but then calling ui.include_js(path, method="link_files") for each file. Here, the multiple calls to include_js are redundant, as the directory and it's children are copied multiple times. However, this redundancy is necessary to create the script tags for each file to be included.

My initial approach was to patch maybe_copy_file to comment out the call to shutil.rmtree on line 230. Worked fine, but this seemed to be over-kill! A better approach may be to rm only the file being included. Or to allow a list of files to be sent to include_js.

Hope this helps!

@rkaramc
Copy link

rkaramc commented Oct 5, 2024

Update to the above... another approach...

I added a __init__.js to the js directory - all it contains are import statements for each of the other js files. Then called ui.include_js("js/__init__.py", method="link_files"). So there is only one call to ui.include_js, and the js directory and it's files are not being rmtree'ed and copied over repeatedly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants