|
105 | 105 | "class CaptureShell(InteractiveShell):\n", |
106 | 106 | " displayhook_class = _CustDisplayHook\n", |
107 | 107 | "\n", |
108 | | - " def __init__(self, path:str|Path=None, mpl_format='retina', history=False, timeout=None):\n", |
| 108 | + " def __init__(self, path:str|Path=None, mpl_format='retina', history=False, timeout:Optional[int]=None):\n", |
109 | 109 | " super().__init__()\n", |
110 | 110 | " self.history_manager.enabled = history\n", |
111 | 111 | " self.timeout = timeout\n", |
|
119 | 119 | " self.run_cell(f\"set_matplotlib_formats('{mpl_format}')\")\n", |
120 | 120 | "\n", |
121 | 121 | " def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,\n", |
122 | | - " stdout=True, stderr=True, display=True, timeout=None):\n", |
| 122 | + " stdout=True, stderr=True, display=True, timeout:Optional[int]=None):\n", |
123 | 123 | " if not timeout: timeout = self.timeout\n", |
124 | 124 | " # TODO what if there's a comment?\n", |
125 | 125 | " semic = raw_cell.rstrip().endswith(';')\n", |
|
706 | 706 | "def run(self:CaptureShell,\n", |
707 | 707 | " code:str, # Python/IPython code to run\n", |
708 | 708 | " stdout=True, # Capture stdout and save as output?\n", |
709 | | - " stderr=True): # Capture stderr and save as output?\n", |
| 709 | + " stderr=True, # Capture stderr and save as output?\n", |
| 710 | + " timeout:Optional[int]=None): # Shell command will time out after {timeout} seconds\n", |
710 | 711 | " \"Run `code`, returning a list of all outputs in Jupyter notebook format\"\n", |
711 | | - " res = self.run_cell(code, stdout=stdout, stderr=stderr)\n", |
| 712 | + " res = self.run_cell(code, stdout=stdout, stderr=stderr, timeout=timeout)\n", |
712 | 713 | " self.result = res.result.result\n", |
713 | 714 | " self.exc = res.exception\n", |
714 | 715 | " return _out_nb(res, self.display_formatter)" |
|
863 | 864 | "o" |
864 | 865 | ] |
865 | 866 | }, |
| 867 | + { |
| 868 | + "cell_type": "code", |
| 869 | + "execution_count": null, |
| 870 | + "metadata": {}, |
| 871 | + "outputs": [ |
| 872 | + { |
| 873 | + "data": { |
| 874 | + "text/plain": [ |
| 875 | + "[{'name': 'stdout', 'output_type': 'stream', 'text': ['no timeout\\n']}]" |
| 876 | + ] |
| 877 | + }, |
| 878 | + "execution_count": null, |
| 879 | + "metadata": {}, |
| 880 | + "output_type": "execute_result" |
| 881 | + } |
| 882 | + ], |
| 883 | + "source": [ |
| 884 | + "s.run(\"import time; time.sleep(0.1); print('no timeout')\", timeout=1)" |
| 885 | + ] |
| 886 | + }, |
| 887 | + { |
| 888 | + "cell_type": "code", |
| 889 | + "execution_count": null, |
| 890 | + "metadata": {}, |
| 891 | + "outputs": [ |
| 892 | + { |
| 893 | + "data": { |
| 894 | + "text/plain": [ |
| 895 | + "['\\x1b[0;31m---------------------------------------------------------------------------\\x1b[0m\\n',\n", |
| 896 | + " '\\x1b[0;31mTimeoutError\\x1b[0m Traceback (most recent call last)\\n']" |
| 897 | + ] |
| 898 | + }, |
| 899 | + "execution_count": null, |
| 900 | + "metadata": {}, |
| 901 | + "output_type": "execute_result" |
| 902 | + } |
| 903 | + ], |
| 904 | + "source": [ |
| 905 | + "o = s.run(\"import time; time.sleep(1.1)\", timeout=1)\n", |
| 906 | + "o[0]['text'][:2]" |
| 907 | + ] |
| 908 | + }, |
| 909 | + { |
| 910 | + "cell_type": "code", |
| 911 | + "execution_count": null, |
| 912 | + "metadata": {}, |
| 913 | + "outputs": [], |
| 914 | + "source": [ |
| 915 | + "#| export\n", |
| 916 | + "@patch\n", |
| 917 | + "async def run_async(self:CaptureShell,\n", |
| 918 | + " code: str, # Python/IPython code to run\n", |
| 919 | + " stdout=True, # Capture stdout and save as output?\n", |
| 920 | + " stderr=True, # Capture stderr and save as output?\n", |
| 921 | + " timeout:Optional[int]=None): # Shell command will time out after {timeout} seconds\n", |
| 922 | + " return self.run(code, stdout=stdout, stderr=stderr, timeout=timeout)" |
| 923 | + ] |
| 924 | + }, |
| 925 | + { |
| 926 | + "cell_type": "code", |
| 927 | + "execution_count": null, |
| 928 | + "metadata": {}, |
| 929 | + "outputs": [ |
| 930 | + { |
| 931 | + "data": { |
| 932 | + "text/plain": [ |
| 933 | + "[{'data': {'text/plain': ['2']},\n", |
| 934 | + " 'metadata': {},\n", |
| 935 | + " 'output_type': 'execute_result'}]" |
| 936 | + ] |
| 937 | + }, |
| 938 | + "execution_count": null, |
| 939 | + "metadata": {}, |
| 940 | + "output_type": "execute_result" |
| 941 | + } |
| 942 | + ], |
| 943 | + "source": [ |
| 944 | + "await s.run_async(\"1+1\")" |
| 945 | + ] |
| 946 | + }, |
| 947 | + { |
| 948 | + "cell_type": "code", |
| 949 | + "execution_count": null, |
| 950 | + "metadata": {}, |
| 951 | + "outputs": [ |
| 952 | + { |
| 953 | + "data": { |
| 954 | + "text/plain": [ |
| 955 | + "[{'name': 'stdout', 'output_type': 'stream', 'text': ['no timeout\\n']}]" |
| 956 | + ] |
| 957 | + }, |
| 958 | + "execution_count": null, |
| 959 | + "metadata": {}, |
| 960 | + "output_type": "execute_result" |
| 961 | + } |
| 962 | + ], |
| 963 | + "source": [ |
| 964 | + "await s.run_async(\"import time; time.sleep(0.1); print('no timeout')\", timeout=1)" |
| 965 | + ] |
| 966 | + }, |
| 967 | + { |
| 968 | + "cell_type": "code", |
| 969 | + "execution_count": null, |
| 970 | + "metadata": {}, |
| 971 | + "outputs": [ |
| 972 | + { |
| 973 | + "data": { |
| 974 | + "text/plain": [ |
| 975 | + "['\\x1b[0;31m---------------------------------------------------------------------------\\x1b[0m\\n',\n", |
| 976 | + " '\\x1b[0;31mTimeoutError\\x1b[0m Traceback (most recent call last)\\n']" |
| 977 | + ] |
| 978 | + }, |
| 979 | + "execution_count": null, |
| 980 | + "metadata": {}, |
| 981 | + "output_type": "execute_result" |
| 982 | + } |
| 983 | + ], |
| 984 | + "source": [ |
| 985 | + "await s.run_async(\"import time; time.sleep(1.1);\", timeout=1)\n", |
| 986 | + "o[0]['text'][:2]" |
| 987 | + ] |
| 988 | + }, |
866 | 989 | { |
867 | 990 | "cell_type": "code", |
868 | 991 | "execution_count": null, |
|
2010 | 2133 | ], |
2011 | 2134 | "metadata": { |
2012 | 2135 | "kernelspec": { |
2013 | | - "display_name": "python3", |
| 2136 | + "display_name": "env", |
2014 | 2137 | "language": "python", |
2015 | | - "name": "python3" |
| 2138 | + "name": "env" |
2016 | 2139 | } |
2017 | 2140 | }, |
2018 | 2141 | "nbformat": 4, |
|
0 commit comments