Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #8
- Loading branch information
Showing
8 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ twine | |
|
||
# Syntax checking | ||
flake8==2.4.1 | ||
|
||
# Soft deps | ||
IPython |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
"""doitlive IPython support.""" | ||
from __future__ import absolute_import | ||
|
||
from IPython.utils import py3compat | ||
from IPython.utils.warn import warn | ||
from IPython.terminal.interactiveshell import TerminalInteractiveShell | ||
from IPython.terminal.ipapp import TerminalIPythonApp | ||
from IPython.utils.text import num_ini_spaces | ||
|
||
|
||
from doitlive import magictype, wait_for, RETURNS | ||
from click import echo | ||
|
||
|
||
class PlayerTerminalInteractiveShell(TerminalInteractiveShell): | ||
"""A magic Ipython terminal shell.""" | ||
def __init__(self, commands, speed, *args, **kwargs): | ||
self.commands = commands | ||
self.current_command = 0 # Index of current command | ||
self.speed = speed | ||
super(PlayerTerminalInteractiveShell, self).__init__(*args, **kwargs) | ||
|
||
# Override raw_input to do magic-typing | ||
# NOTE: Much of this is copy-and-pasted from the parent class's implementation | ||
# due to lack of hooks | ||
def raw_input(self, prompt=''): | ||
if self.current_command > len(self.commands) - 1: | ||
echo('Do you really want to exit ([y]/n)? ', nl=False) | ||
wait_for(RETURNS) | ||
self.ask_exit() | ||
return '' | ||
# raw_input expects str, but we pass it unicode sometimes | ||
prompt = py3compat.cast_bytes_py2(prompt) | ||
|
||
try: | ||
command = self.commands[self.current_command] | ||
magictype(command, prompt_template=prompt, speed=self.speed) | ||
line = py3compat.cast_unicode_py2(command) | ||
except ValueError: | ||
warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | ||
" or sys.stdout.close()!\nExiting IPython!\n") | ||
self.ask_exit() | ||
return "" | ||
|
||
# Try to be reasonably smart about not re-indenting pasted input more | ||
# than necessary. We do this by trimming out the auto-indent initial | ||
# spaces, if the user's actual input started itself with whitespace. | ||
if self.autoindent: | ||
if num_ini_spaces(line) > self.indent_current_nsp: | ||
line = line[self.indent_current_nsp:] | ||
self.indent_current_nsp = 0 | ||
|
||
self.current_command += 1 | ||
return line | ||
|
||
class PlayerTerminalIPythonApp(TerminalIPythonApp): | ||
"""IPython app that runs the PlayerTerminalInteractiveShell.""" | ||
commands = tuple() | ||
speed = 1 | ||
|
||
# Ignore command line args, since this will be run from the doitlive CLI | ||
def parse_command_line(self, argv=None): | ||
return None | ||
|
||
def init_shell(self): | ||
"""initialize the InteractiveShell instance""" | ||
self.shell = PlayerTerminalInteractiveShell.instance( | ||
commands=self.commands, speed=self.speed, | ||
parent=self, display_banner=False, profile_dir=self.profile_dir, | ||
ipython_dir=self.ipython_dir, user_ns=self.user_ns | ||
) | ||
self.shell.configurables.append(self) | ||
|
||
def start_ipython_player(commands, speed=1): | ||
"""Starts a new magic IPython shell.""" | ||
PlayerTerminalIPythonApp.commands = commands | ||
PlayerTerminalIPythonApp.speed = speed | ||
PlayerTerminalIPythonApp.launch_instance() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
echo 'Lets run some python' | ||
|
||
```python | ||
fruits = ['Banana', 'Apple', 'Lime'] | ||
loud_fruits = [fruit.upper() for fruit in fruits] | ||
``` | ||
echo 'We can also run in ipython mode' | ||
```ipython | ||
def fib(n): | ||
a, b = 0, 1 | ||
while a < n: | ||
print(a, end=' ') | ||
a, b = b, a+b | ||
print() | ||
# Magic! | ||
% time fib(100) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters