Repository: davidesarra/jupyter_spaces

Issue:
Automatic console output
It would be great if space cells would automatically print their return result similar to a normal cell. Having to use the print statement for output is cumbersome.

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15?src=pr&el=h1) Report
> Merging [#15](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15?src=pr&el=desc) (73db2ee) into [master](https://codecov.io/gh/davidesarra/jupyter_spaces/commit/035e984dab3d6b29e46db982619a376c3c293b6a?el=desc) (035e984) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15/graphs/tree.svg?width=650&height=150&src=pr&token=4dEKIP6cP0)](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master       #15   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines           72        79    +7     
=========================================
+ Hits            72        79    +7     
```


| [Impacted Files](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [jupyter\_spaces/space.py](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15/diff?src=pr&el=tree#diff-anVweXRlcl9zcGFjZXMvc3BhY2UucHk=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15?src=pr&el=footer). Last update [035e984...73db2ee](https://codecov.io/gh/davidesarra/jupyter_spaces/pull/15?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index 398cbb9..222a688 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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
diff --git a/jupyter_spaces/space.py b/jupyter_spaces/space.py
index 476a7a0..7c3b89e 100644
--- a/jupyter_spaces/space.py
+++ b/jupyter_spaces/space.py
@@ -1,3 +1,6 @@
+import ast
+import sys
+
 from jupyter_spaces.errors import RegistryError
 
 
@@ -101,7 +104,16 @@ class Space:
         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):


Test patch:
diff --git a/tests/test_magics.py b/tests/test_magics.py
index e24cc1b..22da064 100644
--- a/tests/test_magics.py
+++ b/tests/test_magics.py
@@ -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)")