-
Notifications
You must be signed in to change notification settings - Fork 43
Add download button for csv, png, svg #420
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
Open
joehart2001
wants to merge
7
commits into
main
Choose a base branch
from
save-tables-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ac3740
download button for csv, png, svg
joehart2001 5671717
move donwlaod to top left of table
joehart2001 b71f1d4
snazz up button, fix alignments
joehart2001 5e6eede
organise javascript separately
joehart2001 312cad3
commit js and css scripts
joehart2001 1219de2
clean up and add comments
joehart2001 31338b6
update gitignore
joehart2001 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
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 |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * Dash clientside callback for PNG/SVG table export. | ||
| * | ||
| * Python registers this as table_download.captureTable. The callback receives a | ||
| * request from register_download_callbacks, captures the actual Dash table already | ||
| * drawn in the browser, and returns a dcc.Download-compatible payload. | ||
| * | ||
| * This intentionally captures the browser's drawn table instead of rebuilding an | ||
| * image from table data. Rebuiling the table is much much easier, but results in an | ||
| * image that doenst actually look anything like our ml-peg tables. | ||
| * Dash has already applied style_data_conditional, | ||
| * tooltip/header changes, warning colours, column widths, and CSS assets, so | ||
| * html-to-image can export the same visual table the user sees. | ||
| */ | ||
| window.dash_clientside = Object.assign({}, window.dash_clientside, { | ||
| table_download: { | ||
| captureTable: function (request) { | ||
| const dash = window.dash_clientside; | ||
| const noUpdate = dash ? dash.no_update : null; | ||
| if (!request) { | ||
| return noUpdate; | ||
| } | ||
|
|
||
| // register_download_callbacks passes the Dash DataTable component id. | ||
| // Capturing this existing page element preserves the current appearance, | ||
| // including conditional cell colours and any user-adjusted table state. | ||
| const tableNode = document.getElementById(request.element_id); | ||
| if (!tableNode) { | ||
| return noUpdate; | ||
| } | ||
|
|
||
| const source = | ||
| "https://cdn.jsdelivr.net/npm/html-to-image@1.11.11/dist/html-to-image.min.js"; | ||
|
|
||
| // Load html-to-image only when the user asks for an image export. Cache the | ||
| // promise so repeated downloads do not append duplicate script tags. | ||
| const ensureLib = () => { | ||
| if (window.htmlToImage) { | ||
| return Promise.resolve(window.htmlToImage); | ||
| } | ||
| if (window._mlpegHtmlToImagePromise) { | ||
| return window._mlpegHtmlToImagePromise; | ||
| } | ||
| window._mlpegHtmlToImagePromise = new Promise((resolve, reject) => { | ||
| const script = document.createElement("script"); | ||
| script.src = source; | ||
| script.async = true; | ||
| script.onload = () => resolve(window.htmlToImage); | ||
| script.onerror = () => reject(new Error("Failed to load html-to-image")); | ||
| document.head.appendChild(script); | ||
| }); | ||
| return window._mlpegHtmlToImagePromise; | ||
| }; | ||
|
|
||
| const fmt = (request.format || "png").toLowerCase(); | ||
| const filename = request.filename || `table.${fmt}`; | ||
| const basePixelRatio = window.devicePixelRatio || 1; | ||
|
|
||
| // A higher PNG pixel ratio keeps text and colour blocks crisp in the export. | ||
| // SVG is vector output, so the browser pixel ratio is enough there. | ||
| const options = { | ||
| cacheBust: true, | ||
| pixelRatio: fmt === "png" ? Math.max(3, basePixelRatio * 1.5) : basePixelRatio, | ||
| backgroundColor: "#ffffff", | ||
| }; | ||
|
|
||
| return ensureLib() | ||
| .then((htmlToImage) => { | ||
| if (!htmlToImage) { | ||
| throw new Error("html-to-image unavailable"); | ||
| } | ||
| // html-to-image reads the table already drawn by the browser, including | ||
| // computed styles, so the export matches the live Dash table instead of | ||
| // just the raw table values. | ||
| if (fmt === "svg") { | ||
| return htmlToImage.toSvg(tableNode, options); | ||
| } | ||
| return htmlToImage.toPng(tableNode, options); | ||
| }) | ||
| .then((dataUrl) => { | ||
| // html-to-image returns a data URL. Dash downloads need the payload split | ||
| // into content plus metadata. | ||
| const parts = String(dataUrl || "").split(","); | ||
| if (parts.length < 2) { | ||
| return noUpdate; | ||
| } | ||
|
|
||
| if (fmt === "svg") { | ||
| const content = decodeURIComponent(parts.slice(1).join(",")); | ||
| return { | ||
| content: content, | ||
| filename: filename, | ||
| type: "image/svg+xml", | ||
| }; | ||
| } | ||
|
|
||
| // PNG content remains base64 encoded so dcc.Download can write it as bytes. | ||
| return { | ||
| base64: true, | ||
| content: parts.slice(1).join(","), | ||
| filename: filename, | ||
| type: "image/png", | ||
| }; | ||
| }) | ||
| .catch((error) => { | ||
| console.error("Table export failed", error); | ||
| return noUpdate; | ||
| }); | ||
| }, | ||
| }, | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* Dash loads this file as an asset. These rules support the table export UI. */ | ||
|
|
||
| /* Normalise Dash Dropdown internals so it aligns with the Download button. */ | ||
| .table-download-format { | ||
| box-sizing: border-box; | ||
| height: 30px; | ||
| } | ||
|
|
||
| .table-download-format .Select { | ||
| box-sizing: border-box; | ||
| min-height: 30px; | ||
| height: 30px; | ||
| } | ||
|
|
||
| .table-download-format .Select-control { | ||
| box-sizing: border-box; | ||
| min-height: 30px; | ||
| height: 30px; | ||
| border: 1px solid #8aa1b4; | ||
| border-radius: 6px; | ||
| box-shadow: none; | ||
| } | ||
|
|
||
| .table-download-format .Select-placeholder, | ||
| .table-download-format .Select--single > .Select-control .Select-value { | ||
| line-height: 28px; | ||
| color: #243746; | ||
| padding-left: 10px; | ||
| } | ||
|
|
||
| .table-download-format .Select-input { | ||
| height: 28px; | ||
| } | ||
|
|
||
| .table-download-format .Select-input > input { | ||
| line-height: 28px; | ||
| padding-top: 0; | ||
| padding-bottom: 0; | ||
| } | ||
|
|
||
| .table-download-format .Select-arrow-zone { | ||
| width: 24px; | ||
| padding-top: 0; | ||
| padding-bottom: 0; | ||
| } | ||
|
|
||
| /* Small primary action used next to the format dropdown. */ | ||
| .table-download-button { | ||
| box-sizing: border-box; | ||
| display: inline-flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| height: 30px; | ||
| min-height: 30px; | ||
| padding: 0 12px; | ||
| border: 1px solid #0b5cad; | ||
| border-radius: 6px; | ||
| background: #0b73d9; | ||
| color: #ffffff; | ||
| font-size: 12px; | ||
| font-weight: 600; | ||
| line-height: 1; | ||
| box-shadow: 0 1px 2px rgb(15 23 42 / 18%); | ||
| cursor: pointer; | ||
| vertical-align: top; | ||
| transition: | ||
| background-color 120ms ease, | ||
| border-color 120ms ease, | ||
| box-shadow 120ms ease; | ||
| } | ||
|
|
||
| /* Interaction states mirror the app's restrained blue action styling. */ | ||
| .table-download-button:hover { | ||
| background: #095fb5; | ||
| border-color: #084f96; | ||
| box-shadow: 0 2px 5px rgb(15 23 42 / 24%); | ||
| } | ||
|
|
||
| .table-download-button:active { | ||
| background: #084f96; | ||
| box-shadow: inset 0 1px 2px rgb(15 23 42 / 28%); | ||
| } | ||
|
|
||
| .table-download-button:focus-visible, | ||
| .table-download-format .Select.is-focused > .Select-control { | ||
| outline: 2px solid #8ec5ff; | ||
| outline-offset: 2px; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Is this loaded automatically? I don't see any references to it
I don't fully understand what this does that isn't done by
container_styleetc below?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.
changing the container style does not edit the size of the buttons so that we can make them the same size as it only controls the outer wrapper layout around the dropdown/butto to the CSS targets the dropdown’s generated inner elements, plus button hover/active/focus states, which can’t be handled from the wrapper style
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.
Dash loads this automatically because ml_peg/app/data is passed as the app’s assets_folder, so CSS files under that directory are picked up without an explicit import