dn-scribe commited on
Commit
6c6e517
·
unverified ·
1 Parent(s): 64767b8
Files changed (1) hide show
  1. app.py +20 -14
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
- # Inject JS to autofill lat/lon
28
- gr.HTML("""
29
- <script>
30
- navigator.geolocation.getCurrentPosition(
31
- (pos) => {
32
- document.querySelector('textarea[aria-label="Latitude"]').value = pos.coords.latitude;
33
- document.querySelector('textarea[aria-label="Longitude"]').value = pos.coords.longitude;
34
- },
35
- (err) => {
36
- alert("Failed to get location: " + err.message);
 
 
 
 
 
 
 
 
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()