Skip to content

Commit

Permalink
Magic detects notebook name
Browse files Browse the repository at this point in the history
  • Loading branch information
c-martinez committed Jan 16, 2017
1 parent 8eb9e41 commit c0b97ee
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 17 deletions.
136 changes: 120 additions & 16 deletions TestRecipyMagic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand All @@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"collapsed": false
},
Expand All @@ -36,13 +36,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"data": {
"application/javascript": [
"\n",
"var kernel = IPython.notebook.kernel;\n",
"var attribs = document.body.attributes;\n",
"var command = \"recipyNotebookName = \" + \"'\"+attribs['data-notebook-name'].value+\"'\";\n",
"console.log(\"Load notebook name...\")\n",
"kernel.execute(command);"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"!recipy search test.npy"
"%loadNotebookName"
]
},
{
Expand All @@ -52,13 +70,53 @@
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No results found\r\n",
"\u001b[0m"
]
}
],
"source": [
"!./recipy-cmd search test.npy"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[RecipyMagic] this is when recipy should bet imported, not before...\n",
"[RecipyMagic] notebookName: TestRecipyMagic.ipynb\n",
"recipy run inserted, with ID 04a14e50-7447-400d-91aa-428090ae1850\n",
"recipy run inserted, with ID 12edf0d4-6b89-4645-8c37-57f5061f282b\n"
]
}
],
"source": [
"%recipyOn"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {
"collapsed": false
},
Expand All @@ -69,7 +127,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {
"collapsed": false
},
Expand All @@ -83,18 +141,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No results found\n",
"\u001b[0m"
]
}
],
"source": [
"!recipy search test.npy"
"!./recipy-cmd search test.npy"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {
"collapsed": false
},
Expand All @@ -105,14 +172,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1mRun ID:\u001b[0m 12edf0d4-6b89-4645-8c37-57f5061f282b\r\n",
"\u001b[1mCreated by\u001b[0m neocarlitos on 2017-01-16 20:00:21 UTC\r\n",
"\u001b[1mRan\u001b[0m TestRecipyMagic.ipynb using /home/neocarlitos/workspace/Python/recipy/venv/bin/python\r\n",
"Using command-line arguments: -f /run/user/1000/jupyter/kernel-b2e338b7-9803-4eb7-bf93-3cd1f65af266.json\r\n",
"\u001b[1mEnvironment:\u001b[0m Linux-4.7.9-200.fc24.x86_64-x86_64-with-fedora-24-Twenty_Four, python 2.7.12 (default, Sep 29 2016, 13:30:34) \r\n",
"\u001b[1mLibraries:\u001b[0m recipy v0.3.0, numpy v1.11.2\r\n",
"\u001b[1mInputs:\u001b[0m none\r\n",
"\u001b[1mOutputs:\u001b[0m\r\n",
"/home/neocarlitos/workspace/Python/recipy/test.npy (377cfed96fd58963783e45607b9234584c33d551)\r\n",
"\r\n",
"\r\n",
"\u001b[0m"
]
}
],
"source": [
"!recipy search test.npy"
"!./recipy-cmd search test.npy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
5 changes: 4 additions & 1 deletion recipy/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def new_run():
log_init()


def log_init():
def log_init(notebookName=None):
"""Do the initial logging for a new run.
Works out what script has been run, creates a new unique run ID,
Expand All @@ -45,6 +45,9 @@ def log_init():
return
scriptpath = os.path.realpath(sys.argv[1])
cmd_args = sys.argv[2:]
elif notebookName is not None:
scriptpath = notebookName
cmd_args = sys.argv[1:]
else:
scriptpath = os.path.realpath(sys.argv[0])
cmd_args = sys.argv[1:]
Expand Down
38 changes: 38 additions & 0 deletions recipyCommon/magic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from IPython.core.magic import (Magics, magics_class, line_magic,
cell_magic, line_cell_magic)
import time

# The class MUST call this class decorator at creation time
@magics_class
Expand All @@ -8,13 +9,50 @@ def __init__(self, shell):
super(RecipyMagic, self).__init__(shell)
self.recipyModule = None

def loadNotebookName_js(self):
cell = '''
%%javascript
var kernel = IPython.notebook.kernel;
var attribs = document.body.attributes;
var command = "recipyNotebookName = " + "'"+attribs['data-notebook-name'].value+"'";
console.log("Load notebook name...")
kernel.execute(command);
'''
r = self.shell.run_cell(cell)
# x = 1/0
#return None
return r

def getNotebookName(self):
retries = 5
while retries > 0:
if 'recipyNotebookName' in self.shell.user_ns:
return self.shell.user_ns['recipyNotebookName']
else:
retries -= 1
time.sleep(1)
return None


@line_magic
def loadNotebookName(self, line):
"my line magic"
# print("Full access to the main IPython object:", self.shell)
# print("Variables in the user namespace:", list(self.shell.user_ns.keys()))
self.loadNotebookName_js()
return None

@line_magic
def recipyOn(self, line):
"my line magic"
# print("Full access to the main IPython object:", self.shell)
# print("Variables in the user namespace:", list(self.shell.user_ns.keys()))
notebookName = self.getNotebookName()
print("[RecipyMagic] this is when recipy should bet imported, not before...")
print("[RecipyMagic] notebookName: " + notebookName)
import recipy
recipy.log_init(notebookName='TestRecipyMagic.ipynb')
self.recipyModule = recipy
return None

Expand Down

0 comments on commit c0b97ee

Please sign in to comment.