Created
February 22, 2014 07:05
-
-
Save svaksha/9149935 to your computer and use it in GitHub Desktop.
hack_to_paste_images_to_IPYNB.js
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
http://mail.scipy.org/pipermail/ipython-dev/2014-February/013156.html | |
Juergen Hasch's 15 lines of Javascript code to implement "paste images directly from the clipboard (e.g. screenshots) into the notebook" for Chrome. Slow as hell for anything larger than some 10k. | |
window.addEventListener('paste', function(event){ | |
var cell = IPython.notebook.get_selected_cell(); | |
var items = event.clipboardData.items; | |
for (var i = 0; i < items.length; ++i) { | |
if (items[i].kind == 'file' && items[i].type.indexOf('image/') !== -1) { | |
var blob = items[i].getAsFile(); | |
window.URL = window.URL || window.webkitURL; | |
var blobUrl = window.URL.createObjectURL(blob); | |
var img = document.createElement('img'); | |
img.src = blobUrl; | |
var reader = new FileReader(); | |
reader.onload = ( function(evt) { | |
var new_cell = IPython.notebook.insert_cell_below('markdown'); | |
var str = '<img src="' + evt.target.result + '">'; | |
new_cell.set_text(str); | |
new_cell.edit_mode(); | |
new_cell.execute(); | |
} ); | |
reader.readAsDataURL(blob); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment