Skip to content

Commit baaba32

Browse files
committed
ENH: automatically show last cell output
1 parent 035e984 commit baaba32

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

README.md

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Reloading the extension will remove all spaces.
3737
```python
3838
%%space <space-name>
3939
alpha = 0.50
40-
print(alpha)
40+
alpha
4141
```
4242

4343
When you execute a cell within a space, all references are firstly searched in
@@ -51,33 +51,6 @@ execution equivalent to not using such keyword.
5151
Mutable objects in the user namespace can be altered (e.g. appending an item
5252
to a list).
5353

54-
#### Console output
55-
56-
Conversely to the standard usage of the Python console, you need to print
57-
objects explicitly (e.g. by using `print`).
58-
59-
- No output to console
60-
```python
61-
%%space <space-name>
62-
100
63-
```
64-
- Output to console
65-
```python
66-
%%space <space-name>
67-
print(100)
68-
```
69-
70-
If you want IPython to use more advanced representations, you can do so via
71-
IPython's display library (e.g. display a Pandas dataframe as a HTML table).
72-
73-
```python
74-
%%space <space-name>
75-
from IPython.display import display
76-
from pandas import DataFrame
77-
dataframe = DataFrame(data=[[1, 2]])
78-
display(dataframe)
79-
```
80-
8154
### Remove a space
8255

8356
```python

jupyter_spaces/space.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import ast
2+
import sys
3+
14
from jupyter_spaces.errors import RegistryError
25

36

@@ -101,7 +104,16 @@ def execute(self, source):
101104
Args:
102105
source (str): Source code.
103106
"""
104-
exec(source, self._execution_namespace)
107+
tree = ast.parse(source=source)
108+
109+
interactive_tree = ast.Interactive(body=tree.body)
110+
if sys.version_info > (3, 8):
111+
interactive_tree.type_ignores = tree.type_ignores
112+
113+
compiled_interactive_tree = compile(
114+
source=interactive_tree, filename="<string>", mode="single"
115+
)
116+
exec(compiled_interactive_tree, self._execution_namespace)
105117

106118

107119
class ExecutionNamespace(dict):

tests/test_magics.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ def test_get_spaces_reflects_extension_reload(ip):
152152
assert not ip.user_global_ns["spaces"]
153153

154154

155+
def test_space_outputs_to_console(ip, capsys):
156+
ip.run_cell_magic(magic_name="space", line="tomato", cell="100")
157+
assert capsys.readouterr().out == "100\n"
158+
159+
155160
def test_space_can_print_to_console(ip):
156161
with capture_output() as captured:
157162
ip.run_cell_magic(magic_name="space", line="tomato", cell="print(100)")

0 commit comments

Comments
 (0)