-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add example and docs about using Python libraries in a project (#…
…190) * docs: add example and docs about using Python libraries in a project * docs: fix the Python libraries title * docs: apply suggestions, fixes and wording improvements Co-authored-by: Rafael Fernández López <[email protected]>
- Loading branch information
1 parent
be9e355
commit 27ce361
Showing
7 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
version = 1 | ||
|
||
[[repositories]] | ||
name = "wasmlabs" | ||
url = "https://workers.wasmlabs.dev/repository/v1/index.toml" | ||
|
||
[[repositories.runtimes]] | ||
name = "python" | ||
version = "3.11.1+20230217-1" | ||
tags = [ | ||
"latest", | ||
"3.11", | ||
"3.11.1", | ||
] | ||
status = "active" | ||
extensions = ["py"] | ||
args = [ | ||
"--", | ||
"/src/index.py", | ||
] | ||
|
||
[repositories.runtimes.binary] | ||
url = "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/python%2F3.11.1%2B20230217-15dfbed/python-3.11.1.wasm" | ||
filename = "python.wasm" | ||
|
||
[repositories.runtimes.binary.checksum] | ||
type = "sha256" | ||
value = "66589b289f76bd716120f76f234e4dd663064ed5b6256c92d441d84e51d7585d" | ||
|
||
[repositories.runtimes.polyfill] | ||
url = "https://workers.wasmlabs.dev/repository/v1/files/python/3-1/poly.py" | ||
filename = "poly.py" | ||
|
||
[repositories.runtimes.polyfill.checksum] | ||
type = "sha256" | ||
value = "74d10132b0577a39e4ea30002d4605b7cdfb8f39abca327a45c8b313de7ea304" | ||
|
||
[repositories.runtimes.wrapper] | ||
url = "https://workers.wasmlabs.dev/repository/v1/files/python/3-1/wrapper.txt" | ||
filename = "wrapper.txt" | ||
|
||
[repositories.runtimes.wrapper.checksum] | ||
type = "sha256" | ||
value = "cf1edc5b1427180ec09d18f4d169580379f1b12001f30e330759f9a0f8745357" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Python + libraries example | ||
|
||
Run a Python worker that uses a Python library in Wasm Workers Server. | ||
|
||
## Prerequisites | ||
|
||
* Wasm Workers Server (wws): | ||
|
||
```shell-session | ||
curl -fsSL https://workers.wasmlabs.dev/install | bash | ||
``` | ||
|
||
* Clone the repository: | ||
|
||
```shell-session | ||
git clone https://github.com/vmware-labs/wasm-workers-server.git && | ||
cd ./wasm-workers-server/examples/python-libs | ||
``` | ||
|
||
* Install the Python libraries | ||
|
||
```shell-session | ||
pip3 install -r requirements.txt -t ./_libs | ||
``` | ||
|
||
## Run | ||
|
||
This example runs from the previously cloned repository (See [Prerequisites](#prerequisites)). Make sure you followed all the steps and you're in the `examples/python-libs` folder: | ||
|
||
```shell-session | ||
wws . | ||
``` | ||
|
||
## Resources | ||
|
||
* [Python documentation](https://workers.wasmlabs.dev/docs/languages/python) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
!.gitignore | ||
**/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from bs4 import BeautifulSoup | ||
|
||
html_doc = """<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server</h1> | ||
<p> | ||
This page was generated by a Python file running in WebAssembly. | ||
</p> | ||
</main> | ||
</body> | ||
""" | ||
|
||
def worker(req): | ||
soup = BeautifulSoup(html_doc, 'html.parser') | ||
|
||
res = Response(soup.get_text("</br></br>", True)) | ||
res.headers["x-generated-by"] = "wasm-workers-server" | ||
|
||
return res |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name = "libs" | ||
version = "1" | ||
|
||
[vars] | ||
PYTHONPATH = "/opt/python/libs" | ||
|
||
[[folders]] | ||
from = "./_libs" | ||
to = "/opt/python/libs" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
beautifulsoup4 |