Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
)
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.