Thursday, October 11, 2012

IPython Notebook tutorial with doctest feedback

I'm increasingly amazed at IPython Notebook, and want to use it for an interactive tutorial. I'd like the notebook to be full of exercises for the student to fill out, and to get feedback from the notebook itself - a lot like CodingBat but in IPython Notebook format.

Here's the code: ipython_doctester

Next step - maybe - would be to finish with a scorecard cell summarizing the student's overall progress.

And then... automatically push data on a student's progress up to a webserver on the instructor's machine? Which presents a dashboard showing her where everybody is and what everybody is struggling with...

Like every open-source author, I'm eager for feedback!

This dip into IPython Notebook reminds me of people who suggested years ago that I should integrate SQLPython into IPy. Indeed, writing a SQL-handling extension for IPy sounds really attractive... I wonder how many insomniac nights it would take...

6 comments:

Shawn W. said...

I would love to see SQLPython and IPython notebook together! If you do decide to work on it, please post it on GitHub and mention it here.

Paul Moore said...

Probably the simplest way is in http://ipython.org/ipython-doc/stable/interactive/reference.html under "defining your own magics". It's pretty trivial to set up a %connect line magic and a %sql cell magic.

See https://gist.github.com/3872878 for a hacked up quick example.

Gary said...

Very nice! This could be great for my students.

skm said...
This comment has been removed by the author.
Liso said...

Several years ago when I played with ipython's magic I created next one and still use it (probably with small changes during ipython upgrades). It could be useful also for you:


ip = get_ipython()

def __my_function(self, arg):
   helper="""
___arg='''"""+arg+"""'''
___command=___arg.split()[0]
___argo=___arg[len(___command):].lstrip()
if ___command in locals():
   locals()[___command](___argo)
elif ___command in globals():
   globals()[___command](___argo)
else:
   getattr(__builtins__, ___command)(___argo)
"""
   ip.ex(helper)

ip.define_magic('..', __my_function)


Then I could use for example next line in ipython:

.. p select sysdate from dual

where p is callable object (for example function) with sql statment as argument.

Very simple example if you want to try it:

import cx_Oracle
connection=cx_Oracle.connect( ... connection_string ...)
cursor = connection.cursor()

def p(sql):
   cursor.execute(sql)
   print cursor.fetchall()

.. p select sysdate from dual
[(datetime.datetime(2012, 10, 24, 0, 58, 13),)]

For sure p could be more complex object! :)

You could probably want more compact statement (using global callable object):

.. select sysdate from dual

but I prefer to have more objects with different connections in one terminal/session...

Calvin Giles said...

Great tool.

Can the "Success!" output be turned off?

Can the test be an automatic so the @test decorator isn't needed?

Either way, this is cool!

I tend to prototype in IPython then move code to an ide when it works as desired. This could help me create tests while prototyping instead of delaying it to a later date.