Skip to content

Instantly share code, notes, and snippets.

@jdfreder
Created November 7, 2014 09:42
Show Gist options
  • Select an option

  • Save jdfreder/b4065d765b23e763e75a to your computer and use it in GitHub Desktop.

Select an option

Save jdfreder/b4065d765b23e763e75a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"from pyobjectjs import JSObject\n",
"window = JSObject()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print to the Javascript console from Python"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"console = window.console\n",
"console.log('Hello from Python to Javascript')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Replace Javascript's `console.log` with Python's `print`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"console.log = print\n",
"console.log('Hello from Python to Javascript to Python!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use Javascript to print to Python"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%javascript\n",
"console.log('Hello from Javascript to Python');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use Python to assign random colors to each cell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random\n",
"colors = ['red', 'lime', 'skyblue', 'grey', 'yellow', 'purple']\n",
"notebook = window.IPython.notebook\n",
"for i in range(notebook.ncells()):\n",
" color = colors[random.randint(0,len(colors)-1)]\n",
" notebook.get_cell(i).element.css('background', color)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Revert crazy colors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in range(notebook.ncells()):\n",
" notebook.get_cell(i).element.css('background', '')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Handle DOM events in Python (cell click)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"states = {}\n",
"def bind_events(i=0):\n",
" element = notebook.get_cell(i).element\n",
" def on_click(e):\n",
" if element in states and states[element] == True:\n",
" element.css('background', '') \n",
" states[element] = False\n",
" else:\n",
" element.css('background', 'red') \n",
" states[element] = True\n",
" element.on('click', on_click)\n",
" \n",
"for i in range(notebook.ncells()):\n",
" bind_events(i=i)"
]
}
],
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"mimetype": "text/x-python",
"name": "python",
"pygments_lexer": "ipython2"
},
"signature": "sha256:e71fc26bd2eb271886833bf829941a263a7cc9dadb416cbbe642febcce1596fe"
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment