Use plot title as default filename for "Download plot" button#7828
Use plot title as default filename for "Download plot" button#7828emilykl wants to merge 6 commits into
Conversation
|
Nice! Plotly.newPlot(gd, [{y: [1, 2]}], {title: {text: "."}});Right now it returns |
| // so ignore the title entirely if it contains LaTeX markup | ||
| if (!svgTextUtils.matchTex(plotTitle)) { | ||
| potentialFilename = Lib.slugify(plotTitle, 40); | ||
| } |
There was a problem hiding this comment.
You may also consider adding the subtitle.text if present.
There was a problem hiding this comment.
I'm open to that, but I'm having trouble thinking of a real-world example where adding the subtitle would result in a better filename.
I suppose maybe in the case where you have multiple plots with the same title but different subtitles, that could differentiate the filenames.
Do you have any particular examples in mind?
There was a problem hiding this comment.
Here is a common example where subtitle could be used to provide the time stamp of the graph.
Plotly.newPlot(gd, [{y: [1, 2]}], {title: {text: "Hourly Temp. (°C) Forecast - Montréal", subtitle: {text: "9 June 2026"}}});
Good catch!! That's a wild edge case, I guess I would have expected it to return I suppose |
@archmoj Updated — I added |
| // precompile for speed | ||
| var HTML_TAGS_REGEX = /<[^>]*>/g; // anything contained in < > tags | ||
| var FORBIDDEN_CHARS_REGEX = /[\\/:*?"<>|$`'(){}[\],]/g; // Characters in the set: \/:*?"<>|$`'(){}[], | ||
| var FORBIDDEN_CHARS_REGEX = /[\\/:*?"<>|$%&!@#~.^`'(){}[\],]/g; // Characters in the set: \/:*?"<>|$%&!@#~.^`'(){}[], |
There was a problem hiding this comment.
How about equal sign?
the equal sign ( = ) is classified as a forbidden character in filenames across several operating systems (like MS-DOS) and is strongly discouraged on modern systems like Windows.
There was a problem hiding this comment.
Also + and ; could/should be added to the list.
| .replace(UNICODE_REPLACEMENT_CHAR_REGEX, '') // Drop Unicode replacement chars left by previous step | ||
| .replace(HTML_TAGS_REGEX, ' ') // Remove < > tags, such as <br> (replace with space) | ||
| .replace(FORBIDDEN_CHARS_REGEX, '') // Remove forbidden filename characters | ||
| .toLowerCase() // Lowercase everything |
There was a problem hiding this comment.
I'm not sure about lowering the case.
| function matchTex(str) { | ||
| if (!str) return null; | ||
| return str.match(FIND_TEX); | ||
| }; |
| // Trying to slugify a LaTeX string can result in weird ugly filenames, | ||
| // so ignore the title entirely if it contains LaTeX markup | ||
| if (!svgTextUtils.matchTex(plotTitle)) { | ||
| potentialFilename = Lib.slugify(plotTitle, 40); |
There was a problem hiding this comment.
Could you add a comment explaining the 40 character limit?
There was a problem hiding this comment.
Sure, there's not any particular justification, it's kind of arbitrary. But I can add a comment saying that, so it's clear.
40 might even be a bit too long, 30 or 25 might be better.
There was a problem hiding this comment.
TYPICAL for all locale files: The translations likely need to be updated with this change.
Addresses #5124
layout.title.textas default download filename when neitherconfig.toImageButtonOptions.filenameorgd.fnare defined.ihbarfracdpsidt=-v-frac-hbar^22mnabla^2p.png_fullLayout(so that template values are respected) while ignoring the "Click to add title" placeholderplot-imagerather thannewplotSteps for testing
a.
scatter_marker_line_dash(normal example, filename should match)b.
bar-like_traces_tozero(title is defined in template, filename should match)c.
automargin-small-width(no title, should default toplot-image.png)d.
mathjax(MathJax title, should default toplot-image.png)