Permalink
Browse files

update magic command %snap

  • Loading branch information...
sdpython committed Jul 19, 2015
1 parent 86d9244 commit 06dc1d5e0515b9f7bbd9334f9e2cac1a94248e8c

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Snap - partir d'un projet existant"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"On s'assure d'abord que les fichiers javascript de *Snap* sont accessibles depuis le serveur IPython :"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from code_beatrix.jsscripts import copy_jstool2ipython\n",
"copy_jstool2ipython(\"snap\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Si le résultat est une liste vide, cela signifie que cela a déjà été fait."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div id=\"scratch_div_id_62946div\">\n",
" Snap showing up soon...\n",
" <!--<iframe width=\"1000\" height=\"600\" scrolling=\"auto\">-->\n",
" <canvas id=\"scratch_div_id_62946\" width=\"1000\" height=\"600\" />\n",
" <!-- </iframe>-->\n",
" </div>\n",
" \n",
"<script type=\"text/javascript\" src=\"/static/snap/blocks.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/byob.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/cloud.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/gui.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-bn.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-ca.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-cs.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-de.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-dk.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-el.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-eo.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-es.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-fi.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-fr.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-it.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-ja.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-ja_HIRA.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-kn.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-ko.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-ml.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-nl.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-no.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-pl.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-pt.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-pt_BR.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-ru.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-si.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-sv.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-ta.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-te.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-tw.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lang-zh.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/lists.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/locale.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/morphic.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/objects.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/paint.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/sha512.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/store.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/threads.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/widgets.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/xml.js\"></script>\n",
"<script type=\"text/javascript\" src=\"/static/snap/ypr.js\"></script>\n",
"<script>\n",
" var worldscratch_div_id_62946;\n",
" function loopscratch_div_id_62946() {\n",
" worldscratch_div_id_62946.doOneCycle();\n",
" }\n",
" function start_snapscratch_div_id_62946() {\n",
" var sec = document.getElementsByClassName(\"scratch_div_id_62946div\");\n",
" sec.innerHTML = \"loading...\";\n",
" worldscratch_div_id_62946 = new WorldMorph(document.getElementById('scratch_div_id_62946'));\n",
" worldscratch_div_id_62946.worldCanvas.focus();\n",
" new IDE_Morph().openIn(worldscratch_div_id_62946);\n",
" setInterval(loopscratch_div_id_62946, 1);\n",
" sec.innerHTML = \"\";\n",
" }\n",
" window.setTimeout(start_snapscratch_div_id_62946,500);\n",
" </script>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%snap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -1,5 +1,3 @@
.. issue.
.. _l-donnes_exemple:
@@ -0,0 +1,62 @@
"""
@brief test log(time=0s)
"""
import sys
import os
import unittest
import re
try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src
try:
import pyquickhelper
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper
from pyquickhelper import fLOG, explore_folder_iterfile
from pyquickhelper.ipythonhelper import upgrade_notebook
from src.code_beatrix.jsscripts import copy_jstool2ipython
class TestCopyTools(unittest.TestCase):
def test_copy_tools(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
if "travis" in sys.executable:
return
res = copy_jstool2ipython("snap")
assert isinstance(res, list)
if __name__ == "__main__":
unittest.main()
@@ -7,8 +7,7 @@
import glob
import random
from IPython.core.magic import magics_class, line_magic
from IPython.core.display import HTML, display_html, Javascript
from IPython.core.display import HTML, display_html, Javascript, display_javascript
from pyquickhelper.ipythonhelper import MagicCommandParser, MagicClassWithHelpers
from ..jsscripts.snap import __file__ as location_js_snap
@@ -28,7 +27,7 @@ def snap_parser():
defines the way to parse the magic command ``%snap``
"""
parser = MagicCommandParser(prog="snap",
description='insert a snap window inside a notebook')
description='insert a snap window inside a notebook')
parser.add_argument(
'-f',
'--file',
@@ -39,7 +38,7 @@ def snap_parser():
'-d',
'--div',
type=str,
default="scratch-div-id",
default="scratch_div_id",
help='id for the HTML div')
return parser
@@ -60,11 +59,11 @@ def snap(self, line):
raise NotImplementedError()
iddiv = args.div
if iddiv == "scratch-div-id":
# we should use a static counter but it
# is very unlikely more than one snap will be added to
if iddiv == "scratch_div_id":
# we should use a static counter but it
# is very unlikely more than one snap will be added to
# a notebook
iddiv += "-%d" % random.randint(0,100000)
iddiv += "_%d" % random.randint(0, 100000)
js_path = os.path.dirname(location_js_snap)
files = [os.path.split(_)[-1]
@@ -73,22 +72,34 @@ def snap(self, line):
js_libs = [path + _ for _ in files]
html_src = """
<div id="{0}"> </div>
""".format(iddiv)
test_js = """
var world;
window.onload = function () {
world = new WorldMorph(document.getElementById('__DIV__'));
world.worldCanvas.focus();
new IDE_Morph().openIn(world);
setInterval(loop, 1);
};
function loop() {
world.doOneCycle();
<div id="__DIV__div">
Snap showing up soon...
<!--<iframe width="1000" height="600" scrolling="auto">-->
<canvas id="__DIV__" width="1000" height="600" />
<!-- </iframe>-->
</div>
""".replace("__DIV__", iddiv)
test_js = """<script>
var world__DIV__;
function loop__DIV__() {
world__DIV__.doOneCycle();
}
function start_snap__DIV__() {
var sec = document.getElementsByClassName("__DIV__div");
sec.innerHTML = "loading...";
world__DIV__ = new WorldMorph(document.getElementById('__DIV__'));
world__DIV__.worldCanvas.focus();
new IDE_Morph().openIn(world__DIV__);
setInterval(loop__DIV__, 1);
sec.innerHTML = "";
}
window.setTimeout(start_snap__DIV__,500);
</script>
""".replace("__DIV__", iddiv)
display_html(HTML(data=html_src))
return Javascript(data=test_js, lib=js_libs)
libs = [
'<script type="text/javascript" src="{0}"></script>'.format(l) for l in js_libs]
libs = "\n".join(libs)
return HTML(html_src + "\n" + libs + "\n" + test_js)
def register_scratch_magics():
@@ -0,0 +1,25 @@
"""
@file
@brief Copy files to the proper location
"""
import os
from pyquickhelper import synchronize_folder
def copy_jstool2ipython(tool):
"""
copy a tool to ipython folder
@param tool tool name (snap for example)
@return list of copied files
"""
import IPython
dest = os.path.join(os.path.dirname(IPython.__file__), "html", "static")
src = os.path.join(os.path.dirname(__file__), tool)
if not os.path.exists(src):
raise FileNotFoundError("unable to find tool: " + tool)
dest = os.path.join(dest, tool)
if not os.path.exists(dest):
os.mkdir(dest)
return synchronize_folder(src, dest)

0 comments on commit 06dc1d5

Please sign in to comment.