Skip to content

Instantly share code, notes, and snippets.

@rwcitek
Created February 8, 2023 23:27
Show Gist options
  • Save rwcitek/9ea42682f53c3a01eb5fe759a5c887eb to your computer and use it in GitHub Desktop.
Save rwcitek/9ea42682f53c3a01eb5fe759a5c887eb to your computer and use it in GitHub Desktop.
Playing with PyScripts using Docker

Playing with PyScript using Docker

This was inspired by this article:

https://realpython.com/pyscript-python-in-browser/

Clean up container name space

docker container stop pyscript ; docker container rm pyscript

Start instance as a service

docker container run -d -v $PWD:/tmp/zfoo -p 8000:8000 --name pyscript ubuntu:22.04 sleep inf

Set up instance

docker container exec -i -w /tmp/zfoo pyscript /bin/bash <<'eof'
  apt-get update
  apt-get -y dist-upgrade
  apt-get install -y python3-pip vim tree jq less wget curl
  wget https://pyscript.net/alpha/pyscript.{css,js,py}
eof

Create HTML files

docker container exec -i -w /tmp/zfoo pyscript /bin/bash <<'eof'
<<'eof2' cat > hw.v01.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hello, World!</title>
  <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
  <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
  <py-script>print("Hello, World!")</py-script>
</body>
</html>
eof2

<<'eof2' cat > hw.v02.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hello, World!</title>
  <link rel="stylesheet" href="/pyscript.css" />
  <script defer src="/pyscript.js"></script>
</head>
<body>
  <py-script>print("Hello, World!")</py-script>
</body>
</html>
eof2

eof

Start webserver

docker container exec -w /tmp/zfoo pyscript python3 -m http.server

View page

file:///$PWD/hw.v01.html

http://localhost:8000/hw.v02.html

Start interactive container

docker container exec -w /tmp/zfoo pyscript python3 -m http.server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment