diff --git a/gradio-gps.py b/gradio-gps.py new file mode 100644 index 0000000..ab18585 --- /dev/null +++ b/gradio-gps.py @@ -0,0 +1,41 @@ +import gradio as gr + +# This function receives latitude and longitude from JavaScript +def get_location(latitude, longitude): + if latitude and longitude: + return f"Latitude: {latitude}, Longitude: {longitude}" + else: + return "Location not available or permission denied." + +# Gradio interface with JavaScript for GPS +with gr.Blocks() as iface: + gr.Markdown("## Device GPS Location Finder") + + # JavaScript to get GPS and pass it to the Python function + js = """ + () => { + navigator.geolocation.getCurrentPosition( + position => { + const latitude = position.coords.latitude; + const longitude = position.coords.longitude; + // Return latitude and longitude to Gradio input + gradioApp().querySelector('#latitude input').value = latitude; + gradioApp().querySelector('#longitude input').value = longitude; + gradioApp().querySelector('#submit').click(); + }, + error => { + alert("Geolocation access was denied."); + } + ); + } + """ + latitude = gr.Textbox(label="Latitude", interactive=False, elem_id="latitude", visible=False) + longitude = gr.Textbox(label="Longitude", interactive=False, elem_id="longitude", visible=False) + + # Button to trigger JavaScript and get GPS + gps_button = gr.Button("Get GPS Location", elem_id="submit").click( + get_location, [latitude, longitude], gr.Textbox(label="Result") + ).then(None, [], _js=js) + +# Launch the app +iface.launch() diff --git a/location.html b/location.html new file mode 100644 index 0000000..a533f85 --- /dev/null +++ b/location.html @@ -0,0 +1,54 @@ + + + + + + GPS Location Finder + + + + +

GPS Location Finder

+

Click the button below to get your GPS location.

+ + +

Location will appear here.

+ + + + + diff --git a/streamlit-gps.py b/streamlit-gps.py new file mode 100644 index 0000000..fc008df --- /dev/null +++ b/streamlit-gps.py @@ -0,0 +1,43 @@ +import streamlit as st + +# HTML + JavaScript to get GPS coordinates +js_code = """ + +""" + +st.title("Device GPS Location Finder") +st.write("Click the button below to get your GPS location.") + +# HTML form to send data back to Streamlit +st.components.v1.html( + f""" +
+ + + +
+ """, + height=0, +) + +# Display GPS if data is available +latitude = st.experimental_get_query_params().get("latitude", [None])[0] +longitude = st.experimental_get_query_params().get("longitude", [None])[0] + +if latitude and longitude: + st.write(f"Latitude: {latitude}, Longitude: {longitude}") +else: + st.write("Location not found or permission denied.")