Last active
August 14, 2022 02:17
-
-
Save brandonchinn178/928d6137bfd17961b9584a8f96c18827 to your computer and use it in GitHub Desktop.
IHaskell in Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Run IHaskell with Docker.""" | |
import argparse | |
import json | |
import subprocess | |
import sys | |
import webbrowser | |
from pathlib import Path | |
IHASKELL_IMAGE = "gibiansky/ihaskell" | |
CONTAINER_NAME = "ihaskell" | |
PORT = 8888 | |
def main(): | |
args = parse_args() | |
if args.mount_dir: | |
mount_dir = Path(args.mount_dir).resolve() | |
mount_flags = ["--volume", f"{mount_dir}:/home/jovyan/src"] | |
else: | |
mount_flags = [] | |
proc = subprocess.Popen([ | |
"docker", "run", | |
"--name", CONTAINER_NAME, | |
"--publish", f"{PORT}:8888", | |
*mount_flags, | |
IHASKELL_IMAGE, | |
]) | |
try: | |
# wait for container | |
wait_for_container() | |
# open browser | |
token = get_token() | |
webbrowser.open_new_tab(f"http://localhost:{PORT}?token={token}") | |
# wait on process | |
code = proc.wait() | |
if code != 0: | |
sys.exit(code) | |
finally: | |
subprocess.check_call( | |
["docker", "rm", "-f", CONTAINER_NAME], | |
stdout=subprocess.DEVNULL, | |
) | |
def parse_args(): | |
parser = argparse.ArgumentParser(description=__doc__) | |
parser.add_argument( | |
"mount_dir", | |
help="The directory to mount into the container (default: don't mount anything)", | |
metavar="MOUNT_DIR", | |
nargs="?", | |
) | |
return parser.parse_args() | |
def wait_for_container(): | |
while True: | |
out = subprocess.check_output(["docker", "ps", "-f", f"name={CONTAINER_NAME}", "-q"]) | |
if len(out) > 0: | |
return | |
def get_token(): | |
while True: | |
notebooks_proc = subprocess.check_output([ | |
"docker", "exec", CONTAINER_NAME, | |
"jupyter", "notebook", "list", "--json", | |
]) | |
notebook = notebooks_proc.decode().split("\n")[0] | |
if notebook: | |
return json.loads(notebook)["token"] | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source of truth: https://github.com/brandonchinn178/config/blob/main/bin/ihaskell
Usage:
chmod +x
and put it onPATH
ihaskell --help