Repository navigation
How to reference external JS libraries in a HTML CellRenderer #30
Replies: 3 comments 2 replies
|
Thanks for the detailed writeup and for building the Plotly.NET extension, I'm really excited to see this. The issue with the VS Code extension comes down to Monaco editor's AMD module loader. When Monaco initializes, it puts a global This will affect any UMD library loaded from a CDN (D3, Chart.js, Leaflet, etc.), not just plotly.js. As a workaround until the next release, you can temporarily hide <script>
(function(){
function render(){
Plotly.newPlot('YOUR_DIV_ID', data, layout, config);
}
if(window.Plotly){ render(); return; }
var savedDefine = window.define;
window.define = undefined;
var s = document.createElement('script');
s.src = 'https://cdn.plot.ly/plotly-2.35.2.min.js';
s.onload = function(){ window.define = savedDefine; render(); };
s.onerror = function(){ window.define = savedDefine; };
document.head.appendChild(s);
})();
</script>CellOutput test with this workaround:
Notebook: cdn-js-workaround.verso.zip For the next release (1.0.13), I'm going to remove |
|
While working on a regression fix for the Monaco editor and updating for theme changes I thought back to the image above. Verso does have CSS variables that get updated when VS Code's theme changes. Here's what that looks like with a Plotly chart. verso-theme-updates.mp4There are some styling complexities that come from using this for series colors, but for other parts of the chart UI this may look nice and blends in with VS Code better. I wanted to show you in case you're interested in this idea. Here's the test code I used for the chart. using Verso.Abstractions;
new CellOutput("text/html", @"
<div id='themed-plot' style='width:100%;min-height:450px;'></div>
<script>
(function(){
function v(name){ return getComputedStyle(document.documentElement).getPropertyValue(name).trim(); }
function render(){
var accent = v('--verso-accent-primary');
var accent2 = v('--verso-accent-secondary');
var success = v('--verso-status-success');
var error = v('--verso-status-error');
var bg = v('--verso-cell-output-background');
var fg = v('--verso-cell-output-foreground');
var grid = v('--verso-border-default');
var font = v('--verso-ui-font-family') || 'sans-serif';
var months = ['Jan','Feb','Mar','Apr','May','Jun'];
var traces = [
{name:'Revenue', x:months, y:[42,45,51,48,62,58], type:'scatter', mode:'lines+markers', line:{color:accent, width:2}, marker:{size:7}},
{name:'Expenses', x:months, y:[38,36,40,39,44,42], type:'scatter', mode:'lines+markers', line:{color:accent2, width:2}, marker:{size:7}},
{name:'New Users', x:months, y:[120,145,132,168,155,190], type:'bar', marker:{color:success, opacity:0.7}, yaxis:'y2'},
{name:'Churn', x:months, y:[15,12,18,10,14,9], type:'bar', marker:{color:error, opacity:0.7}, yaxis:'y2'}
];
var layout = {
title:{text:'Dashboard with Verso Theme Colors', font:{family:font, size:16, color:fg}},
paper_bgcolor:bg, plot_bgcolor:bg,
font:{family:font, color:fg},
xaxis:{gridcolor:grid, linecolor:grid, tickfont:{color:fg}},
yaxis:{title:'Amount (k)', gridcolor:grid, linecolor:grid, tickfont:{color:fg}, titlefont:{color:fg}},
yaxis2:{title:'Users', overlaying:'y', side:'right', gridcolor:'transparent', linecolor:grid, tickfont:{color:fg}, titlefont:{color:fg}},
legend:{font:{color:fg}, bgcolor:'transparent'},
margin:{t:50,r:60,b:40,l:60}
};
Plotly.react('themed-plot', traces, layout, {responsive:true});
}
// Re-render when the theme changes (CSS variables update but Plotly holds static color values).
// VS Code: body class changes (vscode-dark/vscode-light).
// Blazor Server: #verso-theme style element content updates.
var themeStyle = document.getElementById('verso-theme');
if (themeStyle) {
new MutationObserver(function(){ if(window.Plotly) render(); }).observe(
themeStyle, {childList:true, characterData:true, subtree:true});
}
new MutationObserver(function(){ if(window.Plotly) render(); }).observe(
document.body, {attributes:true, attributeFilter:['class']}
);
if(window.Plotly){render();return;}
var oldDefine=window.define;
window.define=undefined;
var s=document.createElement('script');
s.src='https://cdn.plot.ly/plotly-2.35.2.min.js';
s.onload=function(){window.define=oldDefine;render();};
s.onerror=function(){window.define=oldDefine;};
document.head.appendChild(s);
})();
</script>") |
|
Hi again. The issue you ran into has been fixed in the 1.0.15 release and the code you started with should work with a minor tweak for it to render: <div id='plotly-abc123def456' style='width:100%;min-height:400px'></div>
<script src='https://cdn.plot.ly/plotly-2.35.2.min.js'></script>
<script>
(function(){
function render(){ Plotly.newPlot('plotly-abc123def456',[{"type":"scatter","x":[1,2,3],"y":[4,5,6]}],{},Object.assign({responsive:true},{})); }
if(window.Plotly){ render(); }
else { document.querySelector("script[src*='plotly']").addEventListener('load', render); }
})();
</script>I'll leave this discussion open for a few more days in case you have further questions. |

Uh oh!
There was an error while loading. Please reload this page.
Hi there, thanks for creating this alternative for the deprecated polyglot notebooks.
We were using Polyglot in the context of teaching data science in F#. In my limited experience so far, general F# usage works great in verso notebooks.
However, for moving our teaching to verso we need interactive charting in the Notebooks. I maintain Plotly.NET, which generates plotly.js chart markup which in polyglot was then rendered by plotly.js in html output by pulling the plotly.js dependency via require.js (which was available in the global context of the notebook)
I managed to create an extension for Plotly.NET that correctly creates the cell output, however it does not render. I suspect this is because the plotly.js reference is not loaded.
A blank html cell that tries to load plotly.js via CDN does also not render, and i cannot save it:
Any guidance on how to pull the reference would be greatly appreciated. If there would be some global context where this reference could be registered once, that would also greatly reduce the size of the output of the individual cell.
Here is the extension: https://github.com/plotly/Plotly.NET/tree/verso-ext/src/Plotly.NET.Verso
And here is the test notebook: https://github.com/plotly/Plotly.NET/tree/verso-ext/tests/VersoTests
All reactions