Skip to content

Commit 3282738

Browse files
Added support for IPython 5.0 in ptipython.
1 parent 7f3efb1 commit 3282738

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

ptpython/ipython.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .python_input import PythonInput, PythonValidator, PythonCompleter
2323
from .eventloop import create_eventloop
24+
from .style import default_ui_style
2425

2526
from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed
2627
from IPython.terminal.ipapp import load_default_config
@@ -59,6 +60,23 @@ def out_tokens(self, cli):
5960
return [(Token.Out, text)]
6061

6162

63+
class IPython5Prompt(PromptStyle):
64+
"""
65+
Style for IPython >5.0, use the prompt_toolkit tokens directly.
66+
"""
67+
def __init__(self, prompts):
68+
self.prompts = prompts
69+
70+
def in_tokens(self, cli):
71+
return self.prompts.in_prompt_tokens(cli)
72+
73+
def in2_tokens(self, cli, width):
74+
return self.prompts.continuation_prompt_tokens(cli)
75+
76+
def out_tokens(self, cli):
77+
return []
78+
79+
6280
class IPythonValidator(PythonValidator):
6381
def __init__(self, *args, **kwargs):
6482
super(IPythonValidator, self).__init__(*args, **kwargs)
@@ -167,8 +185,31 @@ def __init__(self, ipython_shell, *a, **kw):
167185
super(IPythonInput, self).__init__(*a, **kw)
168186
self.ipython_shell = ipython_shell
169187

170-
self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompt_manager)
171-
self.prompt_style = 'ipython'
188+
# Prompt for IPython < 5.0
189+
if hasattr(ipython_shell, 'prompt_manager'):
190+
self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompt_manager)
191+
self.prompt_style = 'ipython'
192+
193+
# Prompt for IPython >=5.0:
194+
if hasattr(ipython_shell, 'prompts'):
195+
self.all_prompt_styles['ipython'] = IPython5Prompt(ipython_shell.prompts)
196+
self.prompt_style = 'ipython'
197+
198+
199+
# UI style for IPython. Add tokens that are used by IPython>5.0
200+
style_dict = {}
201+
style_dict.update(default_ui_style)
202+
style_dict.update({
203+
Token.Prompt: '#009900',
204+
Token.PromptNum: '#00ff00 bold',
205+
Token.OutPrompt: '#990000',
206+
Token.OutPromptNum: '#ff0000 bold',
207+
})
208+
209+
self.ui_styles = {
210+
'default': style_dict,
211+
}
212+
self.use_ui_colorscheme('default')
172213

173214

174215
class InteractiveShellEmbed(_InteractiveShellEmbed):
@@ -200,11 +241,16 @@ def get_globals():
200241

201242
if configure:
202243
configure(ipython_input)
244+
ipython_input.prompt_style = 'ipython' # Don't take from config.
203245

204246
self._cli = CommandLineInterface(
205247
application=ipython_input.create_application(),
206248
eventloop=self._eventloop)
207249

250+
def prompt_for_code(self):
251+
# IPython 5.0 calls `prompt_for_code` instead of `raw_input`.
252+
return self.raw_input(self)
253+
208254
def raw_input(self, prompt=''):
209255
print('')
210256
try:

ptpython/style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def generate_style(python_style, ui_style):
7878
Token.Prompt: 'bold',
7979
Token.Prompt.Dots: 'noinherit',
8080

81-
# (IPython) Prompt: "In [1]:"
81+
# (IPython <5.0) Prompt: "In [1]:"
8282
Token.In: 'bold #008800',
8383
Token.In.Number: '',
8484

0 commit comments

Comments
 (0)