-
-
Notifications
You must be signed in to change notification settings - Fork 633
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
ui.download signature is unintuitive #4104
Comments
Thanks for bringing this up, @marcuslimdw! We definitely see your point. The API has been growing over the past:
The first two points try to hide the problem of serving files before being able to request them. Here NiceGUI wraps underlying server-client concepts that should be irrelevant for a UI framework. But too much implicit magic comes at a cost, for sure. Given the current API, how would you migrate towards a better, more explicit solution? I could imagine
|
IMO, that's not the best solution, because it makes it possible to represent invalid states (what should happen if both I suggest instead the following changes:
This makes it practically impossible to do the wrong thing, while making the API self-documenting, since the type of input you're supposed to provide is right in the method name. Usage would be as follows: # old
ui.download(Path(__file__) / "data.json")
# new
ui.download.from_local_path(Path(__file__) / "data.json")) As an aside, this can also ease testing, because this new |
That's a great idea, @marcuslimdw! This provides a really clean separation between the old and the three new ways to use What do you think about the shorter names
The term "content" would include both strings as well as bytes. Or would |
yes, you're right - "content" is a more appropriate term than "bytes" (I forgot that with this, we'd be able to allow the user to download strings too). other than that, I have a slight preference for " |
Good point, @marcuslimdw. For consistency I didn't want to add
Would you like to create pull request? 🙂 |
@falkoschindler honestly I don't use NiceGUI much any more, but I don't mind creating a PR for this - it may take a while, though! |
Description
This is the
ui.download
API:If
src
is of typebytes
, then it represents the raw contents of the file. However, if it is of typestr
orPath
, then we check if it identifies a valid file; if so, we mount that as a static file. Otherwise, we assume that it is a URL, and send the client to that.This is confusing because most APIs throughout the Python ecosystem (that I've seen, at least) that take
str | bytes
do so understanding that the user may bring their own encoding (and usestr
), or not (and usebytes
), and so, semantically, the two represent the same thing.On the other hand, here,
str
means "file path or URL", andbytes
meansfile contents
, which is super unintuitive IMO - a coworker recently wanted to trigger a download of a JSON object that happened to be astr
, and spent a fair bit of time wondering why it wouldn't work.The most common way I see to deal with this is to have two separate APIs - one that takes a path, and one that takes a body (e.g.
etree.open
vsetree.fromstring
). An alternative is to have separate keyword arguments, something likesource_path
andcontents
, and to raise an error if both are specified.The text was updated successfully, but these errors were encountered: