Skip to content

Commit

Permalink
Add hello world example
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Dec 27, 2021
0 parents commit 545a919
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.zip
4 changes: 4 additions & 0 deletions hello-world/hello_world/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
print("Initializing hello world module")
from .some_funcs import say_hello, repeat_string

__all__ = ["say_hello", "repeat_string"]
5 changes: 5 additions & 0 deletions hello-world/hello_world/some_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def say_hello(x):
print("hello", x)

def repeat_string(x, n):
return x * n
22 changes: 22 additions & 0 deletions hello-world/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/pyodide/dev/full/pyodide.js"></script>
</head>
<body>
Pyodide test page <br>
Open your browser console to see Pyodide output
<script type="text/javascript">
async function main(){
const pkgResponse = fetch("hello_world.zip").then(x => x.arrayBuffer());
window.pyodide = await loadPyodide({
indexURL : "https://cdn.jsdelivr.net/pyodide/dev/full/"
});
const pkgdata = await pkgResponse;
pyodide.unpackArchive(pkgdata, "zip");
window.pkg = pyodide.pyimport("hello_world");
}
main();
</script>
</body>
</html>
54 changes: 54 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import argparse
from contextlib import contextmanager
import http.server
from pathlib import Path
import shutil
import socketserver


class Handler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
super().end_headers()

def send_head(self):
if self.path.endswith(".zip"):
path = Path(self.path)
if str(path).startswith("/"):
path = Path("." + str(path)).resolve()

shutil.make_archive(path.with_suffix(""), "zip", root_dir=path.parent)

return super().send_head()



def make_parser(parser):
parser.description = "Start a server"
parser.add_argument(
"--port", action="store", type=int, default=8000, help="choose the port"
)
return parser


@contextmanager
def server(port):
httpd = socketserver.TCPServer(("", port), Handler)
try:
yield httpd
finally:
httpd.shutdown()


def main(args):
build_dir = args.build_dir
port = args.port
with server(port) as httpd:
print(f"serving from {build_dir} at http://localhost:{port}")
httpd.serve_forever()


if __name__ == "__main__":
parser = make_parser(argparse.ArgumentParser())
args = parser.parse_args()
main(args)

0 comments on commit 545a919

Please sign in to comment.