Skip to content

Instantly share code, notes, and snippets.

@vighneshbirodkar
Created September 20, 2013 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vighneshbirodkar/6640371 to your computer and use it in GitHub Desktop.
Save vighneshbirodkar/6640371 to your computer and use it in GitHub Desktop.
Notebook
from ..Base import Display
from ..Base.Display import DisplayBase
from tornado.web import Application
import tornado
from IPython.core.display import Javascript as JS
from IPython.core.display import display
import os
import threading
import tempfile
class NBDisplay(Display.DisplayBase):
_templateFile = "template.html"
"""
"""
init = False
app = None
staticDir = None
__uidCounter__ = 0
def name(self):
__doc__ = DisplayBase.name.__doc__
return "NotebookDisplay"
def __init__(self,size = (640,480),type_ = Display.DEFAULT,title = "SimpleCV",fit = Display.SCROLL):
if( not NBDisplay.init):
NBDisplay.init = True
NBDisplay.staticDir = tempfile.mkdtemp()
NBDisplay.app = Application(static_path = NBDisplay.staticDir,
static_url_prefix = "/display/")
NBDisplay.app.listen(18109)
threading.Thread(target=tornado.ioloop.IOLoop.instance().start).start()
self.__uid__ = NBDisplay.__uidCounter__
NBDisplay.__uidCounter__ += 1
fn = os.path.dirname(__file__) + os.sep + NBDisplay._templateFile
tmp = open(fn)
raw_lines = tmp.readlines()
tmp.close()
lines = [line.replace('\n','') for line in raw_lines]
template = ''.join(lines)
options = {}
options['width'],options['height'] = size
options['code'] = template
options['id'] = self.getUID()
self.startStr = """
window.disp%(id)s = window.open('','','width=%(width)s,height=%(height)s')
window.disp%(id)s.document.write("%(code)s")
""" % options
print self.startStr
display(JS(self.startStr))
def close(self):
pass
def showImage(self,img):
img.save(NBDisplay.staticDir + os.sep + str(img.getUID()%10) + '.png' )
#print uid%10
options = {}
options['imageID'] = img.getUID()
options['width'] = img.width
options['height'] = img.height
options['displayID'] = self.getUID()
command = "window.disp%(displayID)s.show(%(imageID)s,%(width)s,%(height)s)" % options
print command
display(JS(command))
def mousePosition(self):
"""
**SUMMARY**
Reutrns the mouse pointer potion as a tuple of (x,y), with respect to
the image coordinates
**RETURNS**
An (x,y) mouse postion tuple .
"""
pass
def mousePositionRaw(self):
"""
**SUMMARY**
Reutrns the mouse pointer potion as a tuple of (x,y), with respect to
the display coordinates
**RETURNS**
An (x,y) mouse postion tuple .
"""
pass
def leftDown(self):
"""
**SUMMARY**
Reutrns the position where the left mouse button last went down,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the left mouse button went down.
"""
def leftUp(self):
"""
**SUMMARY**
Reutrns the position where the left mouse button last went up,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the left mouse button went up.
"""
def rightDown(self):
"""
**SUMMARY**
Reutrns the position where the right mouse button last went down,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the right mouse button went down.
"""
def rightUp(self):
"""
**SUMMARY**
Reutrns the position where the right mouse button last went up,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the right mouse button went up.
"""
def middleDown(self):
"""
**SUMMARY**
Reutrns the position where the middle mouse button last went down,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the middle mouse button went down.
"""
def middleUp(self):
"""
**SUMMARY**
Reutrns the position where the middle mouse button last went up,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the middle mouse button went up.
"""
def getUID(self):
return self.__uid__
<!--DO NOT USE DOUBLE QUOTES IN THIS DOCUMENT -->
<html>
<head>
<title>SimpleCV</title>
<script>
function show(id,w,h)
{
id10 = id%10;
var c=document.getElementById('id_canvas');
c.width = w;
c.height = h;
con = c.getContext('2d');
var url = 'http://' + document.domain + ':18109/display/' + id10 + '.png?ref=' + id;
var img = new Image();
img.onload = function() {
con.drawImage(img, 10, 10);
con.stroke();
};
img.src = url;
}
</script>
</head>
<body>
<canvas id='id_canvas' class='canvas'></canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment