Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prototype of an extension to provide acess to virtual python environment on an IPython notebook cell
# -*- coding: utf-8 -*-
import os
import nose.tools as nt
from IPython.core.interactiveshell import InteractiveShell
def setup():
ip = InteractiveShell()
ip.extension_manager.load_extension('virtualenvmagic')
def test_virtualenv():
ip = InteractiveShell()
result = ip.run_cell_magic('virtualenv','my_env','import sys; print sys.version')
nt.assert_true(result.startswith('2.7.3'))
"""
Virtualenv magic
Requires virtualenv and virtualenv Wrapper
"""
import sys
import os
import shlex
from subprocess import Popen, PIPE
from IPython.core.magic import cell_magic
from IPython.core.interactiveshell import InteractiveShell
@cell_magic('virtualenv')
def virtualenv(line, cell):
"""
This magic enables the excution of the code of the cell on a
pre-existing virtualenv.
Usage
=====
To activate this magic just write at the top of the cell:
%% virtualenv my_env
"""
if not os.path.exists(os.environ['WORKON_HOME']+line):
print >> sys.stderr, "Environment {} does no exist.".format(line)
return
env_activate_cmd = 'bash -c source '+os.environ['WORKON_HOME'] + '/{}/bin/activate'.format(line)
cmd = shlex.split(env_activate_cmd) + ["&&","python","-"]
p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True)
out,err = p.communicate(cell)
if err:
print >> sys.stderr, err
return out
if 'WORKON_HOME' in os.environ:
#~ get_ipython().register_magic_function(virtualenv, 'cell')
InteractiveShell().register_magic_function(virtualenv, 'cell')
@fccoelho

This comment has been minimized.

Show comment
Hide comment
@fccoelho

fccoelho Jun 5, 2012

@minrk : Would this be a start of a virtualenv cell?

it requires virtualenv to be installed and the env to exist prior to using this

Owner

fccoelho commented Jun 5, 2012

@minrk : Would this be a start of a virtualenv cell?

it requires virtualenv to be installed and the env to exist prior to using this

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Jun 6, 2012

Rather than calling get_ipython() or InteractiveShell(), an IPython extension should offer a load_ipython_extension(ip) method, which will be called with the shell object when it's loaded. Docs: http://ipython.org/ipython-doc/stable/config/extensions/index.html

(The rationale for this is that we have a long term goal to allow more than one InteractiveShell object in the same process - so we're trying not to treat it like a singleton)

Rather than calling get_ipython() or InteractiveShell(), an IPython extension should offer a load_ipython_extension(ip) method, which will be called with the shell object when it's loaded. Docs: http://ipython.org/ipython-doc/stable/config/extensions/index.html

(The rationale for this is that we have a long term goal to allow more than one InteractiveShell object in the same process - so we're trying not to treat it like a singleton)

@fccoelho

This comment has been minimized.

Show comment
Hide comment
@fccoelho

fccoelho Jun 7, 2012

I'll study other extensions and modify my code to conform.
thanks @takluyver

Owner

fccoelho commented Jun 7, 2012

I'll study other extensions and modify my code to conform.
thanks @takluyver

@fccoelho

This comment has been minimized.

Show comment
Hide comment
@fccoelho

fccoelho Jun 8, 2012

Thanks @turicas for the tip on how to make the bash command work...

Owner

fccoelho commented Jun 8, 2012

Thanks @turicas for the tip on how to make the bash command work...

@turicas

This comment has been minimized.

Show comment
Hide comment
@turicas

turicas Jun 9, 2012

@fccoelho, PEP8, please! :-)

turicas commented Jun 9, 2012

@fccoelho, PEP8, please! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment