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

rx._x.asset improvements #3624

Merged
merged 14 commits into from
Nov 22, 2024
Merged

Conversation

benedikt-bartscher
Copy link
Contributor

Closes #3623

@benedikt-bartscher benedikt-bartscher changed the title wip rx._x.asset improvements rx._x.asset improvements Jul 4, 2024
@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review July 4, 2024 21:53
@abulvenz
Copy link
Contributor

abulvenz commented Jul 5, 2024

@benedikt-bartscher I've had a look at the changes and slept one night on it :)
We have two different use cases that both deal with the asset folder:

  • The file an URL is created for is already in the asset folder. In this case the function prepends the path with "/assets/" and the presence of the file is checked.
  • The external asset use case where the file is located relative to the including .py file and copied only when not ENV_BACKEND_ONLY and the resulting file is returned as link.

On my option the automatic detection can be removed and then immediately raise an exception when shared=True, but the file is not located relative to the caller. A file name collision is very unlikely especially because assets/external is on .gitignore
Otherwise I think the implementation is cool.

One question: Is it wanted, that missing file presence of local assets should raise when ENV_BACKEND_ONLY? I thought that might be the case where no assets folder is present at all?

if not dst_file.exists() and (
not dst_file.is_symlink() or dst_file.resolve() != src_file_shared.resolve()
):
dst_file.symlink_to(src_file_shared)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes me a bit nervous because symlinks are not generally enabled or available to unprivileged users on windows.

can we have a fallback path that just copies the files on windows or if this operation fails

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR did not introduce the symlinks, they were there before. i can address it in another PR

"""Add an asset to the app.
def asset(
path: str,
shared: bool = False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the shared argument is a little bit confusing to me... is shared=False for referencing files that are already in the app's assets/ directory?

i suppose this is to align the API to work with source-relative assets as well as assets that are already in the correct place? But setting the default to False seems to break how existing callers are already using this function to reference source-relative assets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shared=True ist for librarys which want to expose the assets to other python modules/reflex apps. Shared mode expects the files to be relative to the python source file where rx._x.asset is called.

shared=False is for "normal"/"local" assets in reflex. It just returns the serving path and checks if the file really exits.

reflex/experimental/assets.py Outdated Show resolved Hide resolved
@Lendemor Lendemor marked this pull request as draft October 25, 2024 18:09
@Lendemor
Copy link
Collaborator

Marked this as draft until re-synced with main.

@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review October 27, 2024 22:24
Lendemor
Lendemor previously approved these changes Oct 29, 2024
Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benedikt-bartscher i updated this PR to move asset out of the experimental namespace with the new API (shared=False default). The existing rx._x.asset has been modified to call the new API with the previous default to avoid breaking any code.

The old _x name will be removed in a few weeks when 0.7.0 is released.

@benedikt-bartscher
Copy link
Contributor Author

@masenf that's awesome, thank you very much. I will rebase our fork and report if everything still works

@masenf masenf merged commit a548633 into reflex-dev:main Nov 22, 2024
32 checks passed
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

Successfully merging this pull request may close these issues.

Multiple rx._x.asset issues
5 participants