Path: blob/main/examples/reference/widgets/FileDownload.ipynb
3215 views
The FileDownload widget allows downloading a file on the frontend by sending the file data to the browser either on initialization (if embed=True) or when the button is clicked.
Discover more on using widgets to add interactivity to your applications in the how-to guides on interactivity. Alternatively, learn how to set up callbacks and (JS-)links between parameters or how to use them as part of declarative UIs with Param.
Parameters:
For details on other options for customizing the component see the layout and styling how-to guides.
Core
auto(boolean): Whether to download the file with the first click (ifTrue) or only after clicking a second time (ifFalse, enables right-click -> Save as).callback(callable): A callable that returns a file or file-like object (takes precedence overfileif set).embed(boolean): Whether to embed the data on initialization.file(str, Path or file-like object): A path to a file or a file-like object.filename(str): The filename to save the file as.
Display
button_style(str): The button style, either 'solid' or 'outline'.button_type(str): A button theme; should be one of'default'(white),'primary'(blue),'success'(green),'info'(yellow),'light'(light), or'danger'(red)icon(str): An icon to render to the left of the button label. Either an SVG or an icon name which is loaded from tabler-icons.io/.icon_size(str): Size of the icon as a string, e.g. 12px or 1em.label(str): A custom label for the download button (by default uses the filename)name(str): The title of the widget
The FileDownload widget accepts a path to a file or a file-like object (with a .read method) if the latter is provided a filename must also be set. By default (auto=True and embed=False) the file is only transferred to the browser after the button is clicked (this requires a live-server or notebook kernel):
The file data may also be embedded immediately using embed parameter, this allows using the widget even in a static export:
If auto=False is set the file will not be downloaded on the initial click but will change the label from "Transfer " to "Download " once the data has been synced. This offers an opportunity to download using the Save as dialog once the data has been transferred.
The FileDownload widget may also be given a file-like object, e.g. here we save a pandas DataFrame as a CSV to a StringIO object and pass that to the widget:
If you want to generate the file dynamically, e.g. because it depends on the parameters of some widget you can also supply a callback (which may be decorated with the widgets and/or parameters it depends on):
Styles
The color of the FileDownload button can be set by selecting one of the available button_type values and the button_style can be 'solid' or 'outline':
Icons
Like other buttons you can provide an explicit icon, either as a named icon loaded from tabler-icons.io/:
or as an explicit SVG: