-
Notifications
You must be signed in to change notification settings - Fork 116
Add render.download
#967
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
Closed
Closed
Add render.download
#967
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,15 +6,18 @@ | |||||||||||||||||||||||||||||||||||||||||||
| "image", | ||||||||||||||||||||||||||||||||||||||||||||
| "table", | ||||||||||||||||||||||||||||||||||||||||||||
| "ui", | ||||||||||||||||||||||||||||||||||||||||||||
| "download", | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import base64 | ||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||||
| import typing | ||||||||||||||||||||||||||||||||||||||||||||
| import urllib.parse | ||||||||||||||||||||||||||||||||||||||||||||
| from typing import ( | ||||||||||||||||||||||||||||||||||||||||||||
| TYPE_CHECKING, | ||||||||||||||||||||||||||||||||||||||||||||
| Any, | ||||||||||||||||||||||||||||||||||||||||||||
| Callable, | ||||||||||||||||||||||||||||||||||||||||||||
| Literal, | ||||||||||||||||||||||||||||||||||||||||||||
| Optional, | ||||||||||||||||||||||||||||||||||||||||||||
| Protocol, | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -24,7 +27,7 @@ | |||||||||||||||||||||||||||||||||||||||||||
| runtime_checkable, | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| from htmltools import TagChild | ||||||||||||||||||||||||||||||||||||||||||||
| from htmltools import Tag, TagChild | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||||||||||||||||||||||||||||
| from ..session._utils import RenderedDeps | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -33,6 +36,7 @@ | |||||||||||||||||||||||||||||||||||||||||||
| from .. import _utils | ||||||||||||||||||||||||||||||||||||||||||||
| from .. import ui as _ui | ||||||||||||||||||||||||||||||||||||||||||||
| from .._namespaces import ResolvedId | ||||||||||||||||||||||||||||||||||||||||||||
| from ..session._session import DownloadHandler, DownloadInfo | ||||||||||||||||||||||||||||||||||||||||||||
| from ..types import MISSING, MISSING_TYPE, ImgData | ||||||||||||||||||||||||||||||||||||||||||||
| from ._try_render_plot import ( | ||||||||||||||||||||||||||||||||||||||||||||
| PlotSizeInfo, | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -560,3 +564,97 @@ def ui( | |||||||||||||||||||||||||||||||||||||||||||
| ~shiny.ui.output_ui | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| return UiTransformer(_fn) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # ====================================================================================== | ||||||||||||||||||||||||||||||||||||||||||||
| # RenderDownload | ||||||||||||||||||||||||||||||||||||||||||||
| # ====================================================================================== | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def download_button_wrapped(id: str) -> Tag: | ||||||||||||||||||||||||||||||||||||||||||||
| return _ui.download_button(id, label=id) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @output_transformer( | ||||||||||||||||||||||||||||||||||||||||||||
| default_ui=download_button_wrapped | ||||||||||||||||||||||||||||||||||||||||||||
| ) # pyright: ignore[reportGeneralTypeIssues] | ||||||||||||||||||||||||||||||||||||||||||||
| async def DownloadTransformer( | ||||||||||||||||||||||||||||||||||||||||||||
| _meta: TransformerMetadata, | ||||||||||||||||||||||||||||||||||||||||||||
| _fn: DownloadHandler, | ||||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||||
| button_label: Optional[TagChild] = None, | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+574
to
+585
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would work today with:
Suggested change
Definitely will be easier with Barret's new class-based approach. |
||||||||||||||||||||||||||||||||||||||||||||
| filename: Optional[str | Callable[[], str]] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| media_type: None | str | Callable[[], str] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: str = "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> str | None: | ||||||||||||||||||||||||||||||||||||||||||||
| session = _meta.session | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| effective_name = _meta.name | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| session._downloads[effective_name] = DownloadInfo( | ||||||||||||||||||||||||||||||||||||||||||||
| filename=filename, | ||||||||||||||||||||||||||||||||||||||||||||
| content_type=media_type, | ||||||||||||||||||||||||||||||||||||||||||||
| handler=_fn, | ||||||||||||||||||||||||||||||||||||||||||||
| encoding=encoding, | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return f"session/{urllib.parse.quote(session.id)}/download/{urllib.parse.quote(effective_name)}?w=" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||||||||||||||
| def download( | ||||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||||
| filename: Optional[str | Callable[[], str]] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| media_type: None | str | Callable[[], str] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: str = "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> DownloadTransformer.OutputRendererDecorator: | ||||||||||||||||||||||||||||||||||||||||||||
| ... | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||||||||||||||
| def download( | ||||||||||||||||||||||||||||||||||||||||||||
| _fn: DownloadTransformer.ValueFn, | ||||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||||
| filename: Optional[str | Callable[[], str]] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| media_type: None | str | Callable[[], str] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: str = "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> DownloadTransformer.OutputRenderer: | ||||||||||||||||||||||||||||||||||||||||||||
| ... | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def download( | ||||||||||||||||||||||||||||||||||||||||||||
| _fn: DownloadTransformer.ValueFn | None = None, | ||||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||||
| button_label: Optional[TagChild] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| filename: Optional[str | Callable[[], str]] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| media_type: None | str | Callable[[], str] = None, | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: str = "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> DownloadTransformer.OutputRenderer | DownloadTransformer.OutputRendererDecorator: | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| Decorator to register a function to handle a download. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||
| id | ||||||||||||||||||||||||||||||||||||||||||||
| The name of the download. | ||||||||||||||||||||||||||||||||||||||||||||
| filename | ||||||||||||||||||||||||||||||||||||||||||||
| The filename of the download. | ||||||||||||||||||||||||||||||||||||||||||||
| media_type | ||||||||||||||||||||||||||||||||||||||||||||
| The media type of the download. | ||||||||||||||||||||||||||||||||||||||||||||
| encoding | ||||||||||||||||||||||||||||||||||||||||||||
| The encoding of the download. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||
| : | ||||||||||||||||||||||||||||||||||||||||||||
| The decorated function. | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
| return DownloadTransformer( | ||||||||||||||||||||||||||||||||||||||||||||
| _fn, | ||||||||||||||||||||||||||||||||||||||||||||
| DownloadTransformer.params( | ||||||||||||||||||||||||||||||||||||||||||||
| button_label=button_label, | ||||||||||||||||||||||||||||||||||||||||||||
| filename=filename, | ||||||||||||||||||||||||||||||||||||||||||||
| media_type=media_type, | ||||||||||||||||||||||||||||||||||||||||||||
| encoding=encoding, | ||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
button_labelgets passed here, but it currently isn't used, and doesn't end up in the UI. It would need to somehow go to the call todownload_button_wrapped().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work great in #964