-
Notifications
You must be signed in to change notification settings - Fork 60
/
_download_button.py
108 lines (93 loc) · 2.54 KB
/
_download_button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
__all__ = ("download_button", "download_link")
from typing import Optional
from htmltools import tags, Tag, TagChildArg, TagAttrArg, css
from .._docstring import add_example
from .._namespaces import namespaced_id
from .._shinyenv import is_pyodide
@add_example()
def download_button(
id: str,
label: TagChildArg,
icon: TagChildArg = None,
width: Optional[str] = None,
**kwargs: TagAttrArg,
) -> Tag:
"""
Create a download button
Parameters
----------
id
An id for the download.
label
An input label.
icon
An icon to display on the button.
width
The width of the button.
kwargs
Additional attributes for the button.
Returns
-------
A UI element
See Also
--------
~shiny.Session.download
~shiny.ui.download_link
"""
return tags.a(
icon,
label,
{"class": "btn btn-default shiny-download-link", "style": css(width=width)},
id=namespaced_id(id),
# This is a fake link that just results in a 404. It will be replaced by a
# working link after the server side logic runs, so this link will only be
# visited in cases where the user clicks the button too fast, or if the server
# never defines a handler for this download button.
href="session/0/download/missing_download",
target="_blank",
# We can't use `download` in pyodide mode, because the browser chooses not to
# route the download through the service worker in that case. (Observed by
# jcheng on 1/7/2022, using Chrome Version 96.0.4664.110.)
download=None if is_pyodide else True,
**kwargs,
)
@add_example()
def download_link(
id: str,
label: TagChildArg,
icon: TagChildArg = None,
width: Optional[str] = None,
**kwargs: TagAttrArg,
) -> Tag:
"""
Create a download button
Parameters
----------
id
An id for the download.
label
An input label.
icon
An icon to display on the button.
width
The width of the button.
kwargs
Additional attributes for the button.
Returns
-------
A UI element
See Also
--------
~shiny.Session.download
~shiny.ui.download_link
"""
return tags.a(
icon,
label,
{"class": "shiny-download-link", "style": css(width=width)},
id=namespaced_id(id),
href="session/0/download/missing_download",
target="_blank",
download=None if is_pyodide else True,
**kwargs,
)