Skip to content

Instantly share code, notes, and snippets.

@etpinard
Last active February 1, 2021 11:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save etpinard/21eb47feaef0acfde93cea37c6a3710a to your computer and use it in GitHub Desktop.
Save etpinard/21eb47feaef0acfde93cea37c6a3710a to your computer and use it in GitHub Desktop.
Setup plotly.py with a custom plotly.js bundle
node_modules
virtualenvs
bundle.js
temp-plot.html
npm-debug.log

plotly.py with custom plotly.js bundle

Setup plotly.py with a custom plotly.js bundle

Running this thing

Requirements

  • node.js and npm
  • python and pip (python3.5 is recommended)
  • virtualenv (pip install virtualenv)

Step 1: setup dependencies and virtual environment

  • git clone this gist
  • cd into it
  • run npm i
  • run make virtualenv

Step 2: activate virtual environment

Run:

$ souce virtualenvs/custom-plotly/bin/activate

Step 3: install plotly.py in virtual environment

Run

(custom-plotly) $ pip install plotly

Step 4: bundle and copy custom plotly.js bundle to plotly.py

Run

(custom-plotly) $ make custom-plotly

The above assumes that your virtual environment runs python3.5. If that is not the case for you, change the py variable in the makefile.

Step 5: plot !!!

(custom-plotly) $ python plot.py

which creates a temp-plot.html file containing the resulting plotly graph and opens it your default browser 🍻

When done

Deactivate the virtual environment with:

(custom-plotly) $ deactivate
'use strict';
var Plotly = require('plotly.js/lib/core');
Plotly.register([
// trace modules part of the 'official' bundle in 1.10.2
require('plotly.js/lib/bar'),
require('plotly.js/lib/box'),
require('plotly.js/lib/heatmap'),
require('plotly.js/lib/histogram'),
require('plotly.js/lib/histogram2d'),
require('plotly.js/lib/histogram2dcontour'),
require('plotly.js/lib/pie'),
require('plotly.js/lib/contour'),
require('plotly.js/lib/scatter3d'),
require('plotly.js/lib/surface'),
require('plotly.js/lib/mesh3d'),
require('plotly.js/lib/scattergeo'),
require('plotly.js/lib/choropleth'),
require('plotly.js/lib/scattergl'),
require('plotly.js/lib/scatterternary'),
// 'beta' trace modules
require('plotly.js/lib/heatmapgl'),
require('plotly.js/lib/contourgl')
]);
module.exports = Plotly;
dir = virtualenvs/custom-plotly/
py = python3.5
bundle.js:
node_modules/.bin/browserify custom-plotly.js -s Plotly > bundle.js
# ref: https://gist.github.com/evansneath/4582716
virtualenv:
mkdir -p ${dir}
virtualenv ${dir} --no-site-packages
echo "[install]\nuser=false" > ${dir}/pip.conf
# IMPORTANT must be run with active virtualenv
#
# $ source virtualenvs/custom-plotly/bin/activate
# $ pip install plotly
#
custom-plotly: bundle.js
cp -f bundle.js ${dir}/lib/${py}/site-packages/plotly/offline/plotly.min.js
{
"name": "plotly.py-custom-plotly.js-bundle",
"version": "1.0.0",
"description": "Setup plotly.py with a custom plotly.js bundle",
"main": "index.js",
"scripts": {},
"author": "Étienne Tétreault-Pinard",
"license": "MIT",
"dependencies": {
"browserify": "^13.0.1",
"plotly.js": "^1.10.2"
}
}
import random
from plotly.offline import plot
plot([dict(
type='heatmapgl',
z=[ [ random.random() for j in range(100) ] for i in range(100) ]
)],
validate=False
)
@TheLogan
Copy link

TheLogan commented Feb 1, 2021

I've attempted going through this process, but I get stuck on the last step of 1.
The make command wasn't recognized and after some googling I discovered that I needed something called GnuWin32.
After installing that and the make package, I then came across this new error
I run make virtualenv
And the console spits out this

mkdir -p virtualenvs/custom-plotly/
The syntax of the command is incorrect.
make: *** [virtualenv] Error 1

And now I'm kinda stuck..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment