Package: pycosnippets
Python Code SnippetsPyCo Snippetsis a python library that allows you to execute python snippets directly on a python runtime
and catch the output by leveraging a jupyter-notebook runner.The main idea is simple:Get a python code snippet as a string.Programmatically add the snippet to a jupyter-notebook template.Programmatically execute the notebook and inspect the output cells.Example:frompycosnippets.templatesimportTemplate# Python code snippetcode_snippet="""print("Hello, World")"""# Create a notebook from a templatetemplate=Template()notebook=template.add_snippet(code=code_snippet)# Execute the notebook and get the output as jsonnotebook.execute()print(notebook.get_last_outputs())Relevant infoSince we attempt to provide a controlled environment, this package is currently only
available for a specific python version and only a few libraries are supported.For this reason, and to reduce compatibility issues, the Python version and default libraries are defined
according to theDatabricks LTS runtime. We install
the runtime viarhdzmota/databricks-runtimelibrary.Current runtime:databricks-runtime==7.3.dev4InstallationInstall with pip:$ pip install pycosnippetsTest by running:$ pycosnippets hello-worldOutput:[{"data":{"text/plain":["'Hello, Pythonista!'"]},"execution_count":2,"metadata":{},"output_type":"execute_result"}]UsageYou can usepycosnippetsas a python library or a commandline application.CLI ApplicationOnce you installpycosnippets, you can call it via the commandline:By runningpycosnippetsif you install it globally.By runningpython -m pycosnippetsotherwise.$ pycosnippets run-snippet --code "print('Hello')"Expected output:{
    "timestamp_start": 1633303932.235129,
    "timestamp_final": 1633303942.675125,
    "output": [
        {
            "name": "stdout",
            "output_type": "stream",
            "text": [
                "Hello\n"
            ]
        }
    ],
    "metadata": null
}We are using a jupyter-notebook runner to execute the python code. The submitted code gets appended to a
juypter-notebook template (src/pycosnippets/template/default.ipynb) and placed in a temporal directory defined by thePYCO_NOTEBOOK_TEMPLATE_OUTPUT_PATHenvironment variable. You can provide your own templates by changing thePYCO_NOTEBOOK_TEMPLATE_PATHandPYCO_NOTEBOOK_TEMPLATE_NAMEenvironment variables.You can also run github gists by provinding the following info:user: Github userfile: Filename to executegist_id: Gist ID$ pycosnippets run-gist \
    --user rhdzmota \
    --file hello.py \
    --gist-id 9aa444dfb15b5d8a96d1204bf173309aYou can also load gists programmatically in your scripts.
Consider the followinghello.pyscript:pyco.load_gist(u="rhdzmota",g="9aa444dfb15b5d8a96d1204bf173309a",f="hello.py",)hello(world="world")Running the following command should load the gist code and execute the function call:$ pycosnippets run-script --filepath hello.pyExpected output:{
    "timestamp_start": 1633305459.1752648,
    "timestamp_final": 1633305463.0998662,
    "output": [
        {
            "data": {
                "text/plain": [
                    "'Hello, world!'"
                ]
            },
            "execution_count": 2,
            "metadata": {},
            "output_type": "execute_result"
        }
    ],
    "metadata": null
}Python libraryThere are two relevant classes:Template: This class allows you to register code in a jupyter notebook template, execute it, and get the output.Import:from pycosnippets.templates import Template(recommended)PyCo: This is a high-level singleton that attempts to provide all the relevant functionality for executing python snippets.Template usage example:frompycosnippets.templatesimportTemplate# Python code snippetcode_snippet="""print("Hello, World")"""# Create a notebook from a templatetemplate=Template()notebook=template.add_snippet(code=code_snippet)# Execute the notebook and get the output as jsonnotebook.execute()print(notebook.get_last_outputs())PyCo usage example:frompycosnippets.pycoimportPyCo# Python code snippetcode_snippet="""print("Hello, World")"""# Get PyCo singletonpyco=PyCo.instance()# Execute the codeoutput=pyco.run_code(code_snippet=code_snippet)print(output)DevelopmentClone this repo and install locally withpip:$ pip install -e .Test by running:$ pycosnippets hello-worldOutput:[{"data":{"text/plain":["'Hello, Pythonista!'"]},"execution_count":2,"metadata":{},"output_type":"execute_result"}]