Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-grodek-dsai committed Nov 3, 2023
1 parent 099abe8 commit 585e029
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

[Documentation](https://deepsense-ai.github.io/ds-pycontain/)

It is a simple wrapper library around docker python API to make it easier to use in. In particular it was created for langchain isolated repl.
It is a simple wrapper library around docker python API to make it easier to use and to provide Python REPL running in a container.
In particular it was created for langchain isolated python REPL, so agents can run code in isolation.

**Warning**: This package requires docker to be installed and running on the host machine. It also needs more work to make it secure.

This package makes it a bit easier to:

* Build docker images from Dockerfiles or in-memory string.
* Pull docker images from dockerhub (or similar).
* Run docker container to execute a one-off command.
* Run docker container to execute a long-running process and communicate with it.
* Run python commands in a container and get the result.

Project boostraped with ds-template: [https://deepsense-ai.github.io/ds-template/](https://deepsense-ai.github.io/ds-template/).

Expand All @@ -37,6 +41,8 @@ Project boostraped with ds-template: [https://deepsense-ai.github.io/ds-template

## Docker images
```python
from ds_pycontain import DockerImage

# pull or use alpine:latest
image = DockerImage.from_tag("alpine")
# use provided tag to pull/use the image
Expand All @@ -47,6 +53,38 @@ image = DockerImage.from_dockerfile("example/Dockerfile")
image = DockerImage.from_dockerfile("path/to/dir_with_Dockerfile/", name="cow")
```

## Python REPL running in docker container
```python
from ds_pycontain.python_dockerized_repl import PythonContainerREPL

# To start python REPL in container it is easy,
# just be aware that it will take some time to start the container
# and ports might be allocated by OS, so use different port/retry
# if you get error.
repl = PythonContainerREPL(port=7121)

# You can run python commands in the container
# and it will keep state between commands.
out1 = repl.exec("x = [1, 2, 3]")
assert out1 == ""
# Eval returns string representation of the python command
# as it would be in python REPL:
out2 = repl.eval("len(x)")
assert out2 == "3"

# Exec returns captured standard output (stdout)
# so it won't return anything in this case:
out3 = repl.exec("len(x)")
assert out3 == ""
# but exec with print works:
out4 = repl.exec("print(len(x))")
assert out4 == "3\n"

# You can also get error messages if code is wrong:
err = repl.exec("print(x")
assert "SyntaxError" in err
```

# Setup developer environment

To start, you need to setup your local machine.
Expand Down

0 comments on commit 585e029

Please sign in to comment.