Skip to content

Commit 89fafb1

Browse files
authored
Merge pull request #53 from bedrock-engineer/fix-marimo-wasm-requests
Fix marimo wasm requests
2 parents 7b2a538 + 7304954 commit 89fafb1

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

examples/hk_kaitak_ags3/hk_kaitak_ags3_to_brgi_geodb.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# /// script
22
# requires-python = ">=3.12"
33
# dependencies = [
4-
# "bedrock-ge==0.3.1
4+
# "bedrock-ge==0.3.1",
55
# "folium==0.20.0",
66
# "geopandas==1.1.0",
77
# "mapclassify==2.9.0",
88
# "marimo",
99
# "matplotlib==3.10.3",
10-
# "numpy==2.3.1",
11-
# "pandas==2.3.0",
10+
# "pyarrow==20.0.0",
1211
# "pyproj==3.7.1",
1312
# "requests==2.32.4",
1413
# "shapely==2.1.1",
@@ -31,19 +30,14 @@ def _():
3130
import platform
3231
import sys
3332
import zipfile
34-
from pathlib import Path
3533

3634
import folium
3735
import geopandas as gpd
3836
import mapclassify
3937
import marimo as mo
4038
import matplotlib
41-
import numpy as np
42-
import pandas as pd
43-
import requests
44-
from pyproj import CRS, Transformer
45-
from pyproj.crs.crs import CompoundCRS
46-
from shapely import Point, wkt
39+
from pyproj import CRS
40+
from shapely import Point
4741

4842
from bedrock_ge.gi.ags import ags_to_brgi_db_mapping
4943
from bedrock_ge.gi.db_operations import merge_dbs
@@ -52,7 +46,8 @@ def _():
5246
from bedrock_ge.gi.mapper import map_to_brgi_db
5347
from bedrock_ge.gi.write import write_brgi_db_to_file
5448

55-
print(platform.system())
49+
platform_system = platform.system()
50+
print(platform_system)
5651
print(sys.version)
5752
# print(sys.executable)
5853
return (
@@ -67,7 +62,7 @@ def _():
6762
merge_dbs,
6863
mo,
6964
platform,
70-
requests,
65+
platform_system,
7166
write_brgi_db_to_file,
7267
zipfile,
7368
)
@@ -122,13 +117,22 @@ def _(mo):
122117

123118

124119
@app.cell
125-
def _(io, requests):
120+
async def _(io, platform_system):
126121
# Read ZIP from disk after downloading manually
127122
# zip = Path.home() / "Downloads" / "kaitak_ags3.zip"
128123

129124
# Request ZIP from GitHub
130125
raw_githubusercontent_url = "https://raw.githubusercontent.com/bedrock-engineer/bedrock-ge/main/examples/hk_kaitak_ags3/kaitak_ags3.zip"
131-
zip = io.BytesIO(requests.get(raw_githubusercontent_url).content)
126+
# When running this marimo notebook in WebAssembly (WASM, a.k.a. Emscripten), use pyodide to request the data
127+
if platform_system == "Emscripten":
128+
from pyodide.http import pyfetch
129+
130+
response = await pyfetch(raw_githubusercontent_url)
131+
zip = io.BytesIO(await response.bytes())
132+
else:
133+
import requests
134+
135+
zip = io.BytesIO(requests.get(raw_githubusercontent_url).content)
132136
return (zip,)
133137

134138

examples/uk_silvertown_tunnel_ags4/uk_silvertown_tunnel_ags4_to_brgi_geodb.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def _():
2525

2626
import geopandas as gpd
2727
import marimo as mo
28-
import requests
2928
from python_ags4 import AGS4
3029

31-
print(platform.system())
30+
platform_system = platform.system()
31+
print(platform_system)
3232
print(sys.version)
3333
# print(sys.executable)
34-
return AGS4, gpd, io, mo, requests
34+
return AGS4, gpd, io, mo, platform_system
3535

3636

3737
@app.cell(hide_code=True)
@@ -46,7 +46,8 @@ def _(mo):
4646
4747
The GI data for the Silvertown tunnel can be found on the British Geological Survey's ([BGS](https://www.bgs.ac.uk/)) [Deposited data search page](https://webapps.bgs.ac.uk/services/ngdc/accessions/index.html):
4848
[**Title**: Preliminary GI for Silvertown Tunnel
49-
**Description**: 77 exploratory holes carried out for the design of the Silvertown Tunnel, London](https://webapps.bgs.ac.uk/services/ngdc/accessions/index.html?simpleText=silvertown#item162465)
49+
**Description**: 77 exploratory holes carried out for the design of the Silvertown Tunnel, London](https://webapps.bgs.ac.uk/services/ngdc/accessions/index.html?simpleText=silvertown#item162465)
50+
For convenience, the AGS 4 file has also been uploaded to GitHub such that you can have a quick look what this AGS 4 data [looks like 🔎](https://raw.githubusercontent.com/bedrock-engineer/bedrock-ge/refs/heads/main/examples/uk_silvertown_tunnel_ags4/20110770-2021-02-16_1308-Final-6.ags).
5051
5152
The ground model has been published as an [AGSi Ground Model](https://www.ags.org.uk/data-format/agsi-ground-model/) by the Association of Geotechnical & Geoenvironmental Specialists ([AGS](https://www.ags.org.uk/)):
5253
[AGSi Guidance / Example files](https://ags-data-format-wg.gitlab.io/agsi/AGSi_Documentation/Example_Silvertown)
@@ -56,20 +57,22 @@ def _(mo):
5657

5758

5859
@app.cell
59-
def _(requests):
60-
# raw_githubusercontent_url = "https://raw.githubusercontent.com/bedrock-engineer/bedrock-ge/refs/heads/main/examples/uk_silvertown_tunnel_ags4/20110770-2021-02-16_1308-Final-6.ags"
61-
# ags_bytes = requests.get(raw_githubusercontent_url).content
62-
63-
# Preferribly, the data can be directly used from the BGS webservice, but maybe this will not be possible in the marimo playground?
60+
async def _(io, platform_system):
6461
bgs_url = "https://webservices.bgs.ac.uk/accessions/download/162465?fileName=20110770%20-%202021-02-16%201308%20-%20Final%20-%206.ags"
65-
ags_bytes = requests.get(bgs_url).content
66-
ags_bytes
67-
return (ags_bytes,)
62+
# When running this marimo notebook in WebAssembly (WASM, a.k.a. Emscripten), use pyodide to request the data
63+
if platform_system == "Emscripten":
64+
from pyodide.http import pyfetch
65+
response = await pyfetch(bgs_url)
66+
ags = io.BytesIO(await response.bytes())
67+
else:
68+
import requests
69+
ags = io.BytesIO(requests.get(bgs_url).content)
70+
return (ags,)
6871

6972

7073
@app.cell
71-
def _(AGS4, ags_bytes, io):
72-
ags_tables, headings = AGS4.AGS4_to_dataframe(io.BytesIO(ags_bytes))
74+
def _(AGS4, ags):
75+
ags_tables, headings = AGS4.AGS4_to_dataframe(ags)
7376
for group, data in ags_tables.items():
7477
ags_tables[group] = (
7578
AGS4.convert_to_numeric(data).drop(columns=["HEADING"])

0 commit comments

Comments
 (0)