Skip to content

Commit c2c46e9

Browse files
Create inv web.build
This command will build a wheel, copy it in the web directory, and create a file `packageFile` with the name of the wheel. If the correct override param is given, bumble.js will read `packageFile` and load that package.
1 parent 32a41a8 commit c2c46e9

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

tasks.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
# Imports
2121
# -----------------------------------------------------------------------------
2222
import os
23-
23+
import glob
24+
import shutil
25+
import urllib
26+
from pathlib import Path
2427
from invoke import task, call, Collection
2528
from invoke.exceptions import Exit, UnexpectedExit
2629

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

207210

211+
# -----------------------------------------------------------------------------
212+
@task
213+
def web_build(ctx):
214+
# Step 1: build the wheel
215+
build(ctx)
216+
# Step 2: Copy the wheel to the web folder, so the http server can access it
217+
newest_wheel = Path(max(glob.glob('dist/*.whl'), key=lambda f: os.path.getmtime(f)))
218+
shutil.copy(newest_wheel, Path('web/'))
219+
# Step 3: Write wheel's name to web/packageFile
220+
with open(Path('web', 'packageFile'), mode='w') as package_file:
221+
package_file.write(str(Path('/') / newest_wheel.name))
222+
# Step 4: Success!
223+
print('Include ?packageFile=true in your URL!')
224+
225+
208226
# -----------------------------------------------------------------------------
209227
web_tasks.add_task(serve)
228+
web_tasks.add_task(web_build, name="build")

web/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# files created by invoke web.build
2+
*.whl
3+
packageFile

web/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ controller using some other transport (ex: `python apps/hci_bridge.py ws-server:
2424
For HTTP, start an HTTP server with the `web` directory as its
2525
root. You can use the invoke task `inv web.serve` for convenience.
2626

27+
`inv web.build` will build the local copy of bumble and automatically copy the `.whl` file
28+
to the web directory. To use this build, include the param `?packageFile=true` to the URL.
29+
2730
In a browser, open either `scanner/scanner.html` or `speaker/speaker.html`.
2831
You can pass optional query parameters:
2932

33+
* `packageFile=true` will automatically use the bumble package built via the
34+
`inv web.build` command.
3035
* `package` may be set to point to a local build of Bumble (`.whl` files).
3136
The filename must be URL-encoded of course, and must be located under
3237
the `web` directory (the HTTP server won't serve files not under its

web/bumble.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export class Bumble extends EventTarget {
7575
}
7676

7777
// Load the Bumble module
78-
bumblePackage ||= 'bumble';
7978
console.log('Installing micropip');
8079
this.log(`Installing ${bumblePackage}`)
8180
await this.pyodide.loadPackage('micropip');
@@ -166,15 +165,28 @@ export class Bumble extends EventTarget {
166165
}
167166
}
168167

168+
async function getBumblePackage() {
169+
const params = (new URL(document.location)).searchParams;
170+
// First check the packageFile override param
171+
if (params.has('packageFile')) {
172+
return await (await fetch('/packageFile')).text()
173+
}
174+
// Then check the package override param
175+
if (params.has('package')) {
176+
return params.get('package')
177+
}
178+
// If no override params, default to the main package
179+
return 'bumble'
180+
}
181+
169182
export async function setupSimpleApp(appUrl, bumbleControls, log) {
170183
// Load Bumble
171184
log('Loading Bumble');
172185
const bumble = new Bumble();
173186
bumble.addEventListener('log', (event) => {
174187
log(event.message);
175188
})
176-
const params = (new URL(document.location)).searchParams;
177-
await bumble.loadRuntime(params.get('package'));
189+
await bumble.loadRuntime(await getBumblePackage());
178190

179191
log('Bumble is ready!')
180192
const app = await bumble.loadApp(appUrl);

0 commit comments

Comments
 (0)