gpoore / codebraid

Live code in Pandoc Markdown
BSD 3-Clause "New" or "Revised" License
367 stars 13 forks source link

Custom executable / custom language #52

Closed akdor1154 closed 2 years ago

akdor1154 commented 2 years ago

Hey, Thanks for this project, seems to nearly fill exactly what I was looking for.

I am getting stuck on one thing - I'm writing shell code cells, but I'm wanting to use a custom executable/shell for these. For this example let's say I want to use /usr/bin/fish, but full disclosure, what I really want is to get codebraid to run its stuff in a container, by passing the executable podman run -it docker.io/ubuntu:21.10.

When passing executable= to the shell session block as per docs, I get an "executable not found" error:

```{.bash .cb-nb session=dock executable="podman run --name mine -v .:/demo -it ubuntu:21.10"}
set -e
cd /demo/demo
pwd

````markdown
``` {.error .sysConfigError}
SYS CONFIG ERROR in "post.md" near line 88:
Could not find executable "podman run --name mine -v .:/demo -it ubuntu:21.10"

When passing `executable=` to the shell session block as a list because I'm guessing it might get send to subprocess.run, that block doesn't run and future blocks use bash directly.
````markdown
```{.bash .cb-nb session=dock executable=["podman", "run", "--name", "mine", "-v" ".:/demo", "-it", "ubuntu:21.10"]}
set -e
cd /demo/demo
pwd
ls -l # does not run
curl -sL https://nixos.org/nix/install | bash -s -- --no-daemon
pwd
ls -l # executes outside container

Is there some permutation like the above that's meant to work with shell blocks?

If not, is there a way I can set up a new language by writing the config file in my project, without needing to fork codebraid locally?

Simple answer to both might be "no", no worries if so :)

Thanks!
gpoore commented 2 years ago

Currently, executable literally means executable path only, without any arguments. I believe arguments did work before the current version, which largely reimplemented the code execution system but still has a few missing features. I need to either add some additional processing of executable so that arguments work (probably something like shlex.split()), or add a new option for arguments so that you can do something like args=....

akdor1154 commented 2 years ago

sorta related if you end up looking at this - I tried to wrap my executor in a local script, but it looks like this doesn't work either (through no fault of your own)

```{.bash .cb-nb executor="./local.sh"}
echo hi
````markdown
``` {.error .sysConfigError}
SYS CONFIG ERROR in "post.md" near line 88:
Could not find executable "local.sh"

I think this is because of the (surprising to me) behvaiour of `Path.as_posix()` -
```python
import pathlib
p = pathlib.Path("./local.sh")
print(p.as_posix())
# "local.sh"

:(

gpoore commented 2 years ago

The new dev version on GitHub adds a new setting executable_opts. So now you should be able to do something like this (not sure if you need the /usr/bin/fish for your setup):

```{.bash .cb-nb session=dock executable=podman executable_opts="run --name mine -v .:/demo -it ubuntu:21.10 /usr/bin/fish"}
set -e
cd /demo/demo
pwd

I've also changed the handling of `executable`, so things like `executable="./local.sh"` should work now.

Finally, I've added a new settings `args` for specifying command-line arguments that are passed to your code. Code execution now defaults to `{executable} {executable_opts} {source} {args}`, where `{source}` is a temp file containing the code from the code block.

Let me know if these new settings have any significant limitations for the sort of thing you are trying to do.

I'm working on a few unrelated features, and as soon as those are finished there will be a new release and a new version on PyPI, probably within a few days.
akdor1154 commented 2 years ago

Woohoo, that looks pretty general to any situation! Ill have a play with the new release.