fix
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ def get_address(lat, lon):
|
|
| 16 |
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
gr.Markdown("## GPS to Address")
|
| 19 |
-
|
| 20 |
lat = gr.Textbox(label="Latitude", interactive=True)
|
| 21 |
lon = gr.Textbox(label="Longitude", interactive=True)
|
| 22 |
address = gr.Textbox(label="Your Address")
|
|
@@ -24,19 +24,25 @@ with gr.Blocks() as demo:
|
|
| 24 |
btn = gr.Button("Get Address")
|
| 25 |
btn.click(fn=get_address, inputs=[lat, lon], outputs=address)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
gr.
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
-
)
|
| 39 |
-
</script>
|
| 40 |
-
""")
|
| 41 |
|
| 42 |
demo.launch()
|
|
|
|
| 16 |
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
gr.Markdown("## GPS to Address")
|
| 19 |
+
|
| 20 |
lat = gr.Textbox(label="Latitude", interactive=True)
|
| 21 |
lon = gr.Textbox(label="Longitude", interactive=True)
|
| 22 |
address = gr.Textbox(label="Your Address")
|
|
|
|
| 24 |
btn = gr.Button("Get Address")
|
| 25 |
btn.click(fn=get_address, inputs=[lat, lon], outputs=address)
|
| 26 |
|
| 27 |
+
# Hidden load trigger to run JS on page load
|
| 28 |
+
dummy = gr.Textbox(visible=False)
|
| 29 |
+
dummy.load(None, js="""
|
| 30 |
+
() => {
|
| 31 |
+
navigator.geolocation.getCurrentPosition(
|
| 32 |
+
(pos) => {
|
| 33 |
+
const latEl = document.querySelector('textarea[aria-label="Latitude"]');
|
| 34 |
+
const lonEl = document.querySelector('textarea[aria-label="Longitude"]');
|
| 35 |
+
if (latEl && lonEl) {
|
| 36 |
+
latEl.value = pos.coords.latitude;
|
| 37 |
+
lonEl.value = pos.coords.longitude;
|
| 38 |
+
}
|
| 39 |
+
},
|
| 40 |
+
(err) => {
|
| 41 |
+
alert("Failed to get location: " + err.message);
|
| 42 |
+
}
|
| 43 |
+
);
|
| 44 |
+
return '';
|
| 45 |
}
|
| 46 |
+
""", outputs=dummy)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
demo.launch()
|