Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create inv web.build #507

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
# Imports
# -----------------------------------------------------------------------------
import os

import glob
import shutil
import urllib
from pathlib import Path
from invoke import task, call, Collection
from invoke.exceptions import Exit, UnexpectedExit

Expand Down Expand Up @@ -205,5 +208,21 @@ def __init__(self, *args, **kwargs):
server.serve_forever()


# -----------------------------------------------------------------------------
@task
def web_build(ctx):
# Step 1: build the wheel
build(ctx)
# Step 2: Copy the wheel to the web folder, so the http server can access it
newest_wheel = Path(max(glob.glob('dist/*.whl'), key=lambda f: os.path.getmtime(f)))
shutil.copy(newest_wheel, Path('web/'))
# Step 3: Write wheel's name to web/packageFile
with open(Path('web', 'packageFile'), mode='w') as package_file:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be simpler to just copy the built .whl file to just web/bumble.whl rather than go through the indirection of a text file pointing to the built file.
Then the web page can just reference ?package=bumble.whl.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately doing that results in this error message:

packaging.utils.InvalidWheelFilename: Invalid wheel filename (wrong number of parts): bumble

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bummer.

package_file.write(str(Path('/') / newest_wheel.name))
# Step 4: Success!
print('Include ?packageFile=true in your URL!')


# -----------------------------------------------------------------------------
web_tasks.add_task(serve)
web_tasks.add_task(web_build, name="build")
3 changes: 3 additions & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# files created by invoke web.build
*.whl
packageFile
5 changes: 5 additions & 0 deletions web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ controller using some other transport (ex: `python apps/hci_bridge.py ws-server:
For HTTP, start an HTTP server with the `web` directory as its
root. You can use the invoke task `inv web.serve` for convenience.

`inv web.build` will build the local copy of bumble and automatically copy the `.whl` file
to the web directory. To use this build, include the param `?packageFile=true` to the URL.

In a browser, open either `scanner/scanner.html` or `speaker/speaker.html`.
You can pass optional query parameters:

* `packageFile=true` will automatically use the bumble package built via the
`inv web.build` command.
* `package` may be set to point to a local build of Bumble (`.whl` files).
The filename must be URL-encoded of course, and must be located under
the `web` directory (the HTTP server won't serve files not under its
Expand Down
18 changes: 15 additions & 3 deletions web/bumble.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export class Bumble extends EventTarget {
}

// Load the Bumble module
bumblePackage ||= 'bumble';
console.log('Installing micropip');
this.log(`Installing ${bumblePackage}`)
await this.pyodide.loadPackage('micropip');
Expand Down Expand Up @@ -166,15 +165,28 @@ export class Bumble extends EventTarget {
}
}

async function getBumblePackage() {
const params = (new URL(document.location)).searchParams;
// First check the packageFile override param
if (params.has('packageFile')) {
return await (await fetch('/packageFile')).text()
}
// Then check the package override param
if (params.has('package')) {
return params.get('package')
}
// If no override params, default to the main package
return 'bumble'
}

export async function setupSimpleApp(appUrl, bumbleControls, log) {
// Load Bumble
log('Loading Bumble');
const bumble = new Bumble();
bumble.addEventListener('log', (event) => {
log(event.message);
})
const params = (new URL(document.location)).searchParams;
await bumble.loadRuntime(params.get('package'));
await bumble.loadRuntime(await getBumblePackage());

log('Bumble is ready!')
const app = await bumble.loadApp(appUrl);
Expand Down
Loading