{"id":15014908,"url":"https://github.com/fastai/tinykernel","last_synced_at":"2025-04-09T19:23:54.521Z","repository":{"id":41543818,"uuid":"394932189","full_name":"fastai/tinykernel","owner":"fastai","description":"A minimal Python kernel so you can run Python in your Python","archived":false,"fork":false,"pushed_at":"2022-04-17T09:56:45.000Z","size":25,"stargazers_count":39,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T21:22:41.501Z","etag":null,"topics":["embedded","fastai","ipython","kernel","nbdev","nbprocess","python"],"latest_commit_sha":null,"homepage":"https://fastai.github.io/tinykernel/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastai.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-11T09:19:19.000Z","updated_at":"2024-01-04T17:00:14.000Z","dependencies_parsed_at":"2022-09-11T11:21:23.284Z","dependency_job_id":null,"html_url":"https://github.com/fastai/tinykernel","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastai%2Ftinykernel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastai%2Ftinykernel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastai%2Ftinykernel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastai%2Ftinykernel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastai","download_url":"https://codeload.github.com/fastai/tinykernel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248096164,"owners_count":21046989,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["embedded","fastai","ipython","kernel","nbdev","nbprocess","python"],"created_at":"2024-09-24T19:46:15.292Z","updated_at":"2025-04-09T19:23:54.497Z","avatar_url":"https://github.com/fastai.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tinykernel\n\u003e A minimal Python kernel, so you can run Python in your Python.\n\n\nAll the clever stuff in this library is provided by Python's builtin `ast` module and compilation/exec/eval system, along with [IPython](https://ipython.org/)'s `CachingCompiler` which does some [deep magic](https://cprohm.de/article/better-test-output-with-ast-rewriting-and-a-patched-standard-library.html/). `tinykernel` just brings them together with a little glue.\n\n## Install\n\nWith pip:\n\n    pip install tinykernel\n\nWith conda:\n\n    conda install -c fastai tinykernel\n\n## How to use\n\nThis library provides a single class, `TinyKernel`, which is a tiny persistent kernel for Python code:\n\n```python\nk = TinyKernel()\n```\n\nCall it, passing Python code, to have the code executed in a separate Python environment:\n\n```python\nk(\"a=1\")\n```\n\nExpressions return the value of the expression:\n\n```python\nk('a')\n```\n\n\n\n\n    1\n\n\n\nAll variables are persisted across calls:\n\n```python\nk(\"a+=1\")\nk('a')\n```\n\n\n\n\n    2\n\n\n\nMulti-line inputs are supported. If the last line is an expression, it is returned:\n\n```python\nk(\"\"\"import types\nb = types.SimpleNamespace(foo=a)\nb\"\"\")\n```\n\n\n\n\n    namespace(foo=2)\n\n\n\nThe original source code is stored, so `inspect.getsource` works and, tracebacks have full details.\n\n```python\nk(\"\"\"def f(): pass # a comment\nimport inspect\ninspect.getsource(f)\"\"\")\n```\n\n\n\n\n    'def f(): pass # a comment\\n'\n\n\n\nWhen creating a `TinyKernel`, you can pass a dict of globals to initialize the environment:\n\n```python\nk = TinyKernel(glb={'foo':'bar'})\nk('foo*2')\n```\n\n\n\n\n    'barbar'\n\n\n\nPass `name` to customize the string that appears in tracebacks (\"kernel\" by default):\n\n```python\nk = TinyKernel(name='myapp')\ncode = '''def f():\n    return 1/0\nprint(f())'''\ntry: k(code)\nexcept Exception as e: print(traceback.format_exc())\n```\n\n    Traceback (most recent call last):\n      File \"\u003cipython-input-37-8b370e64c5cb\u003e\", line 5, in \u003cmodule\u003e\n        try: k(code)\n      File \"/home/jhoward/git/tinykernel/tinykernel/__init__.py\", line 20, in __call__\n        if expr: return self._run(Expression(expr.value), nm, 'eval')\n      File \"/home/jhoward/git/tinykernel/tinykernel/__init__.py\", line 12, in _run\n        def _run(self, p, nm, mode='exec'): return eval(compiler(p, nm, mode), self.glb)\n      File \"\u003cmyapp--1-57331cf14e08\u003e\", line 3, in \u003cmodule\u003e\n        print(f())\n      File \"\u003cmyapp--1-57331cf14e08\u003e\", line 2, in f\n        return 1/0\n    ZeroDivisionError: division by zero\n    \n\n\n## Acknowledgements\n\nThanks to Christopher Prohm, Matthias Bussonnier, and Aaron Meurer for their helpful insights in [this twitter thread](https://twitter.com/jeremyphoward/status/1424990665746763781).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastai%2Ftinykernel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastai%2Ftinykernel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastai%2Ftinykernel/lists"}