Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
generate plotly.js images using nw.js
Generate plotly.js images using nw.js

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>
<meta charset="utf-8">
<title>plotly.js in nw.js</title>
<script type="text/javascript" src="../../plotly/plotly.js/build/plotly.js"></script>
<script type="text/javascript" src="main.js"></script>
'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';
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)
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');
case 'svg':
function saveToFile(img) {
return new Promise(function(resolve, reject) {
fs.writeFile(outPath, img, function(err) {
if(err) reject(err);
function quit() {
log('generated ' + outPath)
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.