Skip to content

Commit

Permalink
ENH: automatically show last cell output
Browse files Browse the repository at this point in the history
  • Loading branch information
davidesarra committed Nov 22, 2020
1 parent 035e984 commit baaba32
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
29 changes: 1 addition & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Reloading the extension will remove all spaces.
```python
%%space <space-name>
alpha = 0.50
print(alpha)
alpha
```

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

#### Console output

Conversely to the standard usage of the Python console, you need to print
objects explicitly (e.g. by using `print`).

- No output to console
```python
%%space <space-name>
100
```
- Output to console
```python
%%space <space-name>
print(100)
```

If you want IPython to use more advanced representations, you can do so via
IPython's display library (e.g. display a Pandas dataframe as a HTML table).

```python
%%space <space-name>
from IPython.display import display
from pandas import DataFrame
dataframe = DataFrame(data=[[1, 2]])
display(dataframe)
```

### Remove a space

```python
Expand Down
14 changes: 13 additions & 1 deletion jupyter_spaces/space.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import ast
import sys

from jupyter_spaces.errors import RegistryError


Expand Down Expand Up @@ -101,7 +104,16 @@ def execute(self, source):
Args:
source (str): Source code.
"""
exec(source, self._execution_namespace)
tree = ast.parse(source=source)

interactive_tree = ast.Interactive(body=tree.body)
if sys.version_info > (3, 8):
interactive_tree.type_ignores = tree.type_ignores

compiled_interactive_tree = compile(
source=interactive_tree, filename="<string>", mode="single"
)
exec(compiled_interactive_tree, self._execution_namespace)


class ExecutionNamespace(dict):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def test_get_spaces_reflects_extension_reload(ip):
assert not ip.user_global_ns["spaces"]


def test_space_outputs_to_console(ip, capsys):
ip.run_cell_magic(magic_name="space", line="tomato", cell="100")
assert capsys.readouterr().out == "100\n"


def test_space_can_print_to_console(ip):
with capture_output() as captured:
ip.run_cell_magic(magic_name="space", line="tomato", cell="print(100)")
Expand Down

0 comments on commit baaba32

Please sign in to comment.