Created
May 13, 2016 09:30
-
-
Save nacyot/9fac5c92dc1ee47e2f0d47061f521bae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.core.magic import Magics, magics_class, cell_magic | |
from IPython.display import HTML | |
import hashlib | |
import datetime | |
@magics_class | |
class D3jsMagics(Magics): | |
TEMPLATE = """ | |
<div id='cell-%(time_hash)s'></div> | |
<script> | |
require.config({ | |
paths: { | |
d3: "http://d3js.org/d3.v3.min", | |
lodash: "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.8.2/lodash" | |
} | |
}); | |
(function(){ | |
var cell_id = '#cell-%(time_hash)s'; | |
var cell = $(cell_id) | |
require(['d3', 'lodash'], function(d3, _){ | |
%(cell)s | |
}); | |
})(); | |
</script> | |
""" | |
@cell_magic | |
def javascript_d3js(self, line, cell): | |
return HTML(self.TEMPLATE % { "time_hash": self.time_hash(), "cell": cell}) | |
def time_hash(self): | |
return hashlib.md5(datetime.datetime.now().strftime("%a:%d:%b:%Y:%H:%M:%S.%f").encode('utf-8')).hexdigest() | |
def load_ipython_extension(ipython): | |
ipython.register_magics(D3jsMagics) | |
def unload_ipython_extension(ipython): | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.core.magic import Magics, magics_class, cell_magic | |
from IPython.display import HTML | |
import hashlib | |
import datetime | |
import os | |
import sys | |
import shutil | |
import glob | |
import base64 | |
import subprocess | |
@magics_class | |
class ProcessingMagics(Magics): | |
BASE_DIR = "/tmp/" | |
SKETCH_DIR = "/tmp/processing/" | |
SCREENSHOT_DIR = "/tmp/processing_screenshots/" | |
SCREENSHOT_CODE = """ | |
void saveFramePng(){{ | |
saveFrame("{0}-########.png"); | |
}} | |
void exit() {{ | |
saveFramePng(); | |
super.exit(); | |
}} | |
""" | |
KEYPRESSED = """ | |
void keyPressed() {{ | |
if (key==']') saveFramePng(); | |
if (key=='q' || key=='Q') exit(); | |
}} | |
""" | |
SIMPLE_TEMPLATE = """ | |
void setup(){{ | |
{0} | |
}} | |
void draw(){{}} | |
""" | |
IMAGE_TEMPLATE = """ | |
<img style='display: inline; padding:0; margin:0;' src='data:image/png;base64,{0}' width='100px' height='100px' /> | |
""" | |
@cell_magic | |
def processing_simple(self, line, cell): | |
return self.run_code(line, self.SIMPLE_TEMPLATE.format(cell)) | |
@cell_magic | |
def processing(self, line, cell): | |
return self.run_code(line, cell) | |
def run_code(self, line, cell): | |
if not os.path.exists(self.SKETCH_DIR): os.mkdir(self.SKETCH_DIR) | |
if not os.path.exists(self.SCREENSHOT_DIR): os.mkdir(self.SCREENSHOT_DIR) | |
time_hash_ = self.time_hash() | |
file_hash = 'SKETCH_{0}'.format(time_hash_) | |
target_dir = '{0}{1}/'.format(self.SKETCH_DIR, file_hash) | |
screenshot = "{0}{1}".format(self.SCREENSHOT_DIR, time_hash_) | |
os.mkdir(target_dir) | |
with open('{0}{1}/{1}.pde'.format(self.SKETCH_DIR, file_hash), "w") as fp: | |
fp.write(cell) | |
fp.write(self.SCREENSHOT_CODE.format(screenshot)) | |
if cell.find("keyPressed()") == -1: | |
fp.write(self.KEYPRESSED) | |
command = 'processing-java --sketch={0} --run --force'.format(target_dir) | |
try: | |
status = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) | |
except subprocess.CalledProcessError as error: | |
status = "Error> " + command + "<br/><br/>" + error.output.decode('ascii') | |
shutil.rmtree(target_dir) | |
screenshots = glob.glob('{0}{1}-*'.format(self.SCREENSHOT_DIR, time_hash_)) | |
if len(screenshots) > 0: | |
images_tag = '<div>' | |
for screenshot in screenshots: | |
image_data = base64.b64encode(open(screenshot, 'rb').read()).decode('utf-8').replace('\n', '') | |
images_tag += self.IMAGE_TEMPLATE.format(image_data) | |
os.remove(screenshot) | |
images_tag += '</div>' | |
return HTML(images_tag + "\n\n" + status.decode('ascii')) | |
else: | |
return HTML(status) | |
def time_hash(self): | |
return hashlib.md5(datetime.datetime.now().strftime("%a:%d:%b:%Y:%H:%M:%S.%f").encode('utf-8')).hexdigest() | |
def load_ipython_extension(ipython): | |
ipython.register_magics(ProcessingMagics) | |
def unload_ipython_extension(ipython): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment