|
6 | 6 | "metadata": {}, |
7 | 7 | "outputs": [], |
8 | 8 | "source": [ |
9 | | - "#|hide\n", |
10 | | - "#|default_exp shell" |
| 9 | + "#| hide\n", |
| 10 | + "#| default_exp shell" |
11 | 11 | ] |
12 | 12 | }, |
13 | 13 | { |
|
25 | 25 | "metadata": {}, |
26 | 26 | "outputs": [], |
27 | 27 | "source": [ |
28 | | - "#|export\n", |
| 28 | + "#| export\n", |
29 | 29 | "from __future__ import annotations\n", |
30 | 30 | "\n", |
31 | 31 | "from fastcore.utils import *\n", |
|
81 | 81 | "metadata": {}, |
82 | 82 | "outputs": [], |
83 | 83 | "source": [ |
84 | | - "#|export\n", |
| 84 | + "#| export\n", |
85 | 85 | "class _CustDisplayHook(DisplayHook):\n", |
86 | 86 | " def write_output_prompt(self): pass\n", |
87 | 87 | " def write_format_data(self, data, md_dict): pass\n", |
|
100 | 100 | "metadata": {}, |
101 | 101 | "outputs": [], |
102 | 102 | "source": [ |
103 | | - "#|export\n", |
| 103 | + "#| export\n", |
104 | 104 | "class CaptureShell(InteractiveShell):\n", |
105 | 105 | " displayhook_class = _CustDisplayHook\n", |
106 | 106 | "\n", |
|
141 | 141 | "metadata": {}, |
142 | 142 | "outputs": [], |
143 | 143 | "source": [ |
144 | | - "#|export\n", |
| 144 | + "#| export\n", |
145 | 145 | "@patch\n", |
146 | 146 | "def run_cell(self:CaptureShell, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,\n", |
147 | 147 | " stdout=True, stderr=True, display=True, timeout=None, verbose=False):\n", |
|
689 | 689 | "metadata": {}, |
690 | 690 | "outputs": [], |
691 | 691 | "source": [ |
692 | | - "#|export\n", |
| 692 | + "#| export\n", |
693 | 693 | "def _out_stream(text, name): return dict(name=name, output_type='stream', text=text.splitlines(True))\n", |
694 | 694 | "def _out_exc(e):\n", |
695 | 695 | " ename = type(e).__name__\n", |
|
729 | 729 | "metadata": {}, |
730 | 730 | "outputs": [], |
731 | 731 | "source": [ |
732 | | - "#|export\n", |
| 732 | + "#| export\n", |
733 | 733 | "@patch\n", |
734 | 734 | "def run(self:CaptureShell,\n", |
735 | 735 | " code:str, # Python/IPython code to run\n", |
|
1278 | 1278 | "metadata": {}, |
1279 | 1279 | "outputs": [], |
1280 | 1280 | "source": [ |
1281 | | - "#|export\n", |
| 1281 | + "#| export\n", |
1282 | 1282 | "@patch\n", |
1283 | 1283 | "def cell(self:CaptureShell, cell, stdout=True, stderr=True, verbose=False):\n", |
1284 | 1284 | " \"Run `cell`, skipping if not code, and store outputs back in cell\"\n", |
|
1504 | 1504 | "metadata": {}, |
1505 | 1505 | "outputs": [], |
1506 | 1506 | "source": [ |
1507 | | - "#|export\n", |
| 1507 | + "#| export\n", |
1508 | 1508 | "def _false(o): return False\n", |
1509 | 1509 | "\n", |
1510 | 1510 | "@patch\n", |
|
1738 | 1738 | "metadata": {}, |
1739 | 1739 | "outputs": [], |
1740 | 1740 | "source": [ |
1741 | | - "#|export\n", |
| 1741 | + "#| export\n", |
1742 | 1742 | "@patch\n", |
1743 | 1743 | "def execute(self:CaptureShell,\n", |
1744 | 1744 | " src:str|Path, # Notebook path to read from\n", |
|
1805 | 1805 | "metadata": {}, |
1806 | 1806 | "outputs": [], |
1807 | 1807 | "source": [ |
1808 | | - "#|export\n", |
| 1808 | + "#| export\n", |
1809 | 1809 | "@patch\n", |
1810 | 1810 | "def prettytb(self:CaptureShell, \n", |
1811 | 1811 | " fname:str|Path=None): # filename to print alongside the traceback\n", |
|
1880 | 1880 | "metadata": {}, |
1881 | 1881 | "outputs": [], |
1882 | 1882 | "source": [ |
1883 | | - "#|hide\n", |
| 1883 | + "#| hide\n", |
1884 | 1884 | "# Trailing `;` stops data from being captured\n", |
1885 | 1885 | "res = CaptureShell().run('1+1;')\n", |
1886 | 1886 | "test_eq(res, [])\n", |
|
1897 | 1897 | "metadata": {}, |
1898 | 1898 | "outputs": [], |
1899 | 1899 | "source": [ |
1900 | | - "#|hide\n", |
| 1900 | + "#| hide\n", |
1901 | 1901 | "# Streams are split on and keep newlines\n", |
1902 | 1902 | "res = CaptureShell().run(r\"print('a\\nb'); print('c\\n\\n'); print('d')\")\n", |
1903 | 1903 | "test_eq(res[0]['text'], ['a\\n', 'b\\n', 'c\\n', '\\n', '\\n', 'd\\n'])" |
|
1909 | 1909 | "metadata": {}, |
1910 | 1910 | "outputs": [], |
1911 | 1911 | "source": [ |
1912 | | - "#|hide\n", |
| 1912 | + "#| hide\n", |
1913 | 1913 | "# Text mime data are split on and keep newlines\n", |
1914 | 1914 | "res = CaptureShell().run(r\"from IPython.display import Markdown; display(Markdown('a\\nb'))\")\n", |
1915 | 1915 | "test_eq(res[0]['data']['text/markdown'], ['a\\n', 'b'])" |
|
1921 | 1921 | "metadata": {}, |
1922 | 1922 | "outputs": [], |
1923 | 1923 | "source": [ |
1924 | | - "#|hide\n", |
| 1924 | + "#| hide\n", |
1925 | 1925 | "# Binary image mime data are base64-encoded and end in a single `\\n`\n", |
1926 | 1926 | "\n", |
1927 | 1927 | "def _pil2b64(im): return b64encode(im._repr_png_()).decode()+'\\n'\n", |
|
1938 | 1938 | "metadata": {}, |
1939 | 1939 | "outputs": [], |
1940 | 1940 | "source": [ |
1941 | | - "#|hide\n", |
| 1941 | + "#| hide\n", |
1942 | 1942 | "# `display_data` flushes stdout and stderr - in that order\n", |
1943 | 1943 | "res = CaptureShell().run(\"import sys; sys.stderr.write('Error!'); print(1); display(1)\")\n", |
1944 | 1944 | "test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']},\n", |
|
1952 | 1952 | "metadata": {}, |
1953 | 1953 | "outputs": [], |
1954 | 1954 | "source": [ |
1955 | | - "#|hide\n", |
| 1955 | + "#| hide\n", |
1956 | 1956 | "# Consecutive writes to stdout are grouped\n", |
1957 | 1957 | "res = CaptureShell().run('print(1); print(2); print(3)')\n", |
1958 | 1958 | "test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n', '2\\n', '3\\n']}])" |
|
1964 | 1964 | "metadata": {}, |
1965 | 1965 | "outputs": [], |
1966 | 1966 | "source": [ |
1967 | | - "#|hide\n", |
| 1967 | + "#| hide\n", |
1968 | 1968 | "# Consecutive writes to stdout are grouped despite an intermediate write to stderr, which is captured after\n", |
1969 | 1969 | "res = CaptureShell().run(\"import sys; print(1); print(2); sys.stderr.write('Error!'); print(3)\")\n", |
1970 | 1970 | "test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n', '2\\n', '3\\n']},\n", |
|
1977 | 1977 | "metadata": {}, |
1978 | 1978 | "outputs": [], |
1979 | 1979 | "source": [ |
1980 | | - "#|hide\n", |
| 1980 | + "#| hide\n", |
1981 | 1981 | "# `execute_result` is always last\n", |
1982 | 1982 | "res = CaptureShell().run(\"import sys; sys.stderr.write('Error!'); print(1); display(1); 1\")\n", |
1983 | 1983 | "test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']},\n", |
|
2081 | 2081 | "metadata": {}, |
2082 | 2082 | "outputs": [], |
2083 | 2083 | "source": [ |
2084 | | - "#|export\n", |
| 2084 | + "#| export\n", |
2085 | 2085 | "@call_parse\n", |
2086 | 2086 | "def exec_nb(\n", |
2087 | 2087 | " src:str, # Notebook path to read from\n", |
|
2259 | 2259 | "metadata": {}, |
2260 | 2260 | "outputs": [], |
2261 | 2261 | "source": [ |
2262 | | - "#|hide\n", |
| 2262 | + "#| hide\n", |
2263 | 2263 | "import nbdev; nbdev.nbdev_export()" |
2264 | 2264 | ] |
2265 | 2265 | }, |
|
0 commit comments