Skip to content

Commit 7f21c2d

Browse files
committed
#3137 initial code for Plotly support
1 parent 552e9af commit 7f21c2d

File tree

7 files changed

+113
-32
lines changed

7 files changed

+113
-32
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ package-lock.json
1414

1515
# Ignore the vendor file and bundle for alternative installation
1616
vendor/
17-
.bundle/
17+
.bundle/
18+
19+
# Ignore folders related to IDEs
20+
.vscode/

_includes/head/custom.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
<!-- Support for Mermaid -->
2020
<script type="module">
21-
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
22-
mermaid.initialize({startOnLoad:true, theme:'default'});
23-
await mermaid.run({querySelector:'code.language-mermaid'});
21+
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
22+
mermaid.initialize({startOnLoad:true, theme:'default'});
23+
await mermaid.run({querySelector:'code.language-mermaid'});
2424
</script>
2525

26+
<!-- Support for Plotly -->
27+
<script src='https://cdnjs.cloudflare.com/ajax/libs/plotly.js/3.0.1/plotly.min.js'></script>
28+
2629
<!-- end custom head snippets -->

_includes/scripts.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<script src="{{ base_path }}/assets/js/main.min.js"></script>
1+
<script type="module" src="{{ base_path }}/assets/js/main.min.js"></script>
22

33
{% include analytics.html %}

_pages/markdown.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ graph TD;
9292
C-->D;
9393
```
9494

95+
## Plotly
96+
97+
```plotly
98+
{
99+
"data": [
100+
{
101+
"x": [1, 2, 3, 4],
102+
"y": [10, 15, 13, 17],
103+
"type": "scatter"
104+
},
105+
{
106+
"x": [1, 2, 3, 4],
107+
"y": [16, 5, 11, 9],
108+
"type": "scatter"
109+
}
110+
]
111+
}
112+
```
113+
95114
## Markdown guide
96115

97116
Academic Pages uses [kramdown](https://kramdown.gettalong.org/index.html) for Markdown rendering, which has some differences from other Markdown implementations such as GitHub's. In addition to this guide, please see the [kramdown Syntax page](https://kramdown.gettalong.org/syntax.html) for full documentation.

assets/js/_main.js

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
11
/* ==========================================================================
2-
jQuery plugin settings and other scripts
2+
Various functions that we want to use within the template
3+
========================================================================== */
4+
5+
// Determine the expected state of the theme toggle, which can be "dark", "light", or
6+
// "system". Default is "system".
7+
let determineThemeSetting = () => {
8+
let themeSetting = localStorage.getItem("theme");
9+
return (themeSetting != "dark" && themeSetting != "light" && themeSetting != "system") ? "system" : themeSetting;
10+
};
11+
12+
// Determine the computed theme, which can be "dark" or "light". If the theme setting is
13+
// "system", the computed theme is determined based on the user's system preference.
14+
let determineComputedTheme = () => {
15+
let themeSetting = determineThemeSetting();
16+
if (themeSetting != "system") {
17+
return themeSetting;
18+
}
19+
return (userPref && userPref("(prefers-color-scheme: dark)").matches) ? "dark" : "light";
20+
};
21+
22+
// Set the theme on page load or when explicitly called
23+
let setTheme = (theme) => {
24+
const use_theme =
25+
theme ||
26+
localStorage.getItem("theme") ||
27+
$("html").attr("data-theme") ||
28+
browserPref;
29+
30+
if (use_theme === "dark") {
31+
$("html").attr("data-theme", "dark");
32+
$("#theme-icon").removeClass("fa-sun").addClass("fa-moon");
33+
} else if (use_theme === "light") {
34+
$("html").removeAttr("data-theme");
35+
$("#theme-icon").removeClass("fa-moon").addClass("fa-sun");
36+
}
37+
};
38+
39+
// Toggle the theme manually
40+
var toggleTheme = () => {
41+
const current_theme = $("html").attr("data-theme");
42+
const new_theme = current_theme === "dark" ? "light" : "dark";
43+
localStorage.setItem("theme", new_theme);
44+
setTheme(new_theme);
45+
};
46+
47+
/* ==========================================================================
48+
Plotly integration script so that Markdown codeblocks will be rendered
49+
========================================================================== */
50+
51+
// Read the Plotly data from the code block, hide it, and render the chart as new node. This allows for the
52+
// JSON data to be retrieve when the theme is switched.
53+
import { plotlyDarkLayout, plotlyLightLayout } from './theme.js';
54+
document.addEventListener("readystatechange", () => {
55+
if (document.readyState === "complete") {
56+
document.querySelectorAll("pre>code.language-plotly").forEach((elem) => {
57+
// Parse the Plotly JSON data and hide it
58+
var jsonData = JSON.parse(elem.textContent);
59+
elem.parentElement.classList.add("hidden");
60+
61+
// Add the Plotly node
62+
let chartElement = document.createElement("div");
63+
elem.parentElement.after(chartElement);
64+
65+
// Set the theme for the plot and render it
66+
const theme = (determineComputedTheme() === "dark") ? plotlyDarkLayout : plotlyLightLayout;
67+
if (jsonData.layout) {
68+
jsonData.layout.template = (jsonData.layout.template) ? { ...theme, ...jsonData.layout.template } : theme;
69+
} else {
70+
jsonData.layout = { template: theme };
71+
}
72+
Plotly.react(chartElement, jsonData.data, jsonData.layout);
73+
});
74+
}
75+
});
76+
77+
/* ==========================================================================
78+
Actions that should occur when the page has been fully loaded
379
========================================================================== */
480

581
$(document).ready(function () {
@@ -8,23 +84,6 @@ $(document).ready(function () {
884
? 'dark'
985
: 'light';
1086

11-
// Set the theme on page load or when explicitly called
12-
var setTheme = function (theme) {
13-
const use_theme =
14-
theme ||
15-
localStorage.getItem("theme") ||
16-
$("html").attr("data-theme") ||
17-
browserPref;
18-
19-
if (use_theme === "dark") {
20-
$("html").attr("data-theme", "dark");
21-
$("#theme-icon").removeClass("fa-sun").addClass("fa-moon");
22-
} else if (use_theme === "light") {
23-
$("html").removeAttr("data-theme");
24-
$("#theme-icon").removeClass("fa-moon").addClass("fa-sun");
25-
}
26-
};
27-
2887
setTheme();
2988

3089
// if user hasn't chosen a theme, follow OS changes
@@ -36,14 +95,6 @@ $(document).ready(function () {
3695
}
3796
});
3897

39-
// Toggle the theme manually
40-
var toggleTheme = function () {
41-
const current_theme = $("html").attr("data-theme");
42-
const new_theme = current_theme === "dark" ? "light" : "dark";
43-
localStorage.setItem("theme", new_theme);
44-
setTheme(new_theme);
45-
};
46-
4798
$('#theme-toggle').on('click', toggleTheme);
4899

49100
// These should be the same as the settings in _variables.scss

assets/js/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/theme.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)