Skip to content

Instantly share code, notes, and snippets.

@nyanshell
Last active August 29, 2015 14:08
Show Gist options
  • Save nyanshell/70fc1bdc4fa4d46e605d to your computer and use it in GitHub Desktop.
Save nyanshell/70fc1bdc4fa4d46e605d to your computer and use it in GitHub Desktop.
A bokeh image embedding sample.
from __future__ import division
import numpy as np
from bokeh.plotting import *
from bokeh.resources import CDN
from bokeh.embed import components
from bottle import run, template, route, jinja2_view
# NOTE: if you do not have numba installed, comment out this import,
# and the 'autojit' lines below (the example will run more slowly).
# from numba import autojit
# These functions generate the Mandelbrot set image. Don't worry if
# you are not familiar with them. The import thing is just to know
# that they create a 2D array of numbers that we can colormap.
# @autojit
def mandel(x, y, max_iters):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Mandelbrot
set given a fixed number of iterations.
"""
c = complex(x, y)
z = 0.0j
for i in range(max_iters):
z = z*z + c
if (z.real*z.real + z.imag*z.imag) >= 4:
return i
return max_iters
# @autojit
def create_fractal(min_x, max_x, min_y, max_y, image, iters):
height = image.shape[0]
width = image.shape[1]
pixel_size_x = (max_x - min_x) / width
pixel_size_y = (max_y - min_y) / height
for x in range(width):
real = min_x + x * pixel_size_x
for y in range(height):
imag = min_y + y * pixel_size_y
color = mandel(real, imag, iters)
image[y, x] = color
# Define the bounding coordinates to generate the Mandelbrot image in. You
# can play around with these.
min_x = -2.0
max_x = 1.0
min_y = -1.0
max_y = 1.0
# Use the functions above to create a scalar image (2D array of numbers)
img = np.zeros((1024, 1536), dtype = np.uint8)
create_fractal(min_x, max_x, min_y, max_y, img, 20)
# EXERCISE: Fill in the missing parameters to use the `image` renderer to
# display the Mandelbrot image 'img' with the title 'Mandelbrot', colormapped
# with the palette 'Spectral-11', and a fixed range given by (min_x, max_x)
# and (min_y, max_y).
#
# NOTE: the `image` renderer can display many images at once, so it takes
# **lists** of images, coordinates, and palettes. Remember to supply sequences
# for these parameters, even if you are just supply one.
min_x = -2.0
max_x = 1.0
min_y = -1.0
max_y = 1.0
image = image(image=[img],
x=[min_x],
y=[min_y],
dw=[max_x-min_x],
dh=[max_y-min_y],
palette=["Spectral-11"],
x_range = [min_x, max_x],
y_range = [min_y, max_y],
title="Mandelbrot",
tools="pan,wheel_zoom,box_zoom,reset,previewsave",
plot_width=900,
plot_height=600
)
# EXERCISE: use `image_rgba` to display the image above. Use the following
# cordinates: (x0,y0) = (0,0) and (x1,y1) = (10,10). Remember to set the
# x_range and y_range explicitly as above.
script, div = components(image, CDN)
render_template = """<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>Bosondata Analyzer Portal</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="http://cdn.pydata.org/bokeh-0.6.1.min.js"></script>
<link href="http://cdn.pydata.org/bokeh-0.6.1.min.css" rel="stylesheet", type="text/css">
{{script|safe}}
</head>
<body>
========hello==============
{{div}}
========hello!!!===========
</body>
</html>"""
@route('/hello')
@jinja2_view(render_template)
def hello():
return {"div": div, "script": script}
run(host='0.0.0.0', port=8080, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment