Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
generate plotly.js images using nw.js
Generate plotly.js images using nw.js
node_modules
npm-debug.log

plotly.js in nw.js

Generate plotly.js images using nw.js

How to run this thing?

1. Setup

  • Install nw.js (see instructions)
  • Clone this gist and cd into it

2. Generate a graph

./path/to/nw .

or if nw.js is linked to a nw executable

npm start

and 🍻.`

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>plotly.js in nw.js</title>
<script type="text/javascript" src="../../plotly/plotly.js/build/plotly.js"></script>
</head>
<body>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
'use strict';
/* global Plotly:true */
var fs = require('fs');
var gui = require('nw.gui');
var MOCK = '../../plotly/plotly.js/test/image/mocks/gl2d_marker_line_width.json';
var FORMATS = ['svg', 'jpeg', 'png'];
var BASE_FILENAME = 'out';
FORMATS.forEach(generateOne);
function generateOne(format) {
var imageOptions = { format: format, imageDataOnly: true };
var outPath = BASE_FILENAME + '.' + format;
var div = document.createElement('div');
fs.readFile(MOCK, 'utf-8', function(err, raw) {
if(err) throw err;
var fig = JSON.parse(raw);
Plotly.plot(div, fig)
.then(toImage)
.then(decodeImage)
.then(saveToFile)
.then(quit);
});
function toImage(gd) {
return Plotly.toImage(gd, imageOptions);
}
function decodeImage(img) {
return new Promise(function(resolve) {
switch(imageOptions.format) {
case 'png':
case 'jpeg':
img = new Buffer(img, 'base64');
break;
case 'svg':
break;
}
resolve(img);
});
}
function saveToFile(img) {
return new Promise(function(resolve, reject) {
fs.writeFile(outPath, img, function(err) {
if(err) reject(err);
resolve(outPath);
});
});
}
function quit() {
log('generated ' + outPath)
gui.App.quit();
}
}
function log(msg) {
process.stdout.write(msg + '\n');
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{
"name": "nw.js-plotly.js",
"version": "2.0.0",
"description": "generate plotly.js images using nw.js",
"main": "index.html",
"scripts": {
"start": "nw ."
},
"author": "Étienne Tétreault-Pinard",
"license": "MIT",
"dependencies": {
"plotly.js": "^1.16.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.