awacke1 commited on
Commit
d3854a7
1 Parent(s): f1fed16

Update backupapp.py

Browse files
Files changed (1) hide show
  1. backupapp.py +53 -16
backupapp.py CHANGED
@@ -1,16 +1,14 @@
1
- import googlemaps
2
  import streamlit as st
 
 
 
3
  from datetime import datetime
4
 
5
- # Replace your Google Maps API key here
6
- #gmaps = googlemaps.Client(key='Your-Google-Maps-API-Key')
7
- gmaps = googlemaps.Client(key='AIzaSyDybq2mxujekZVivmr03Y5-GGHXesn4TLI')
8
-
9
  def get_directions(source, destination):
10
  now = datetime.now()
11
- # Get all modes: driving, walking, bicycling, and transit
12
- modes = ['driving', 'walking', 'bicycling', 'transit']
13
  directions_info = {}
 
14
  for mode in modes:
15
  directions_result = gmaps.directions(source, destination, mode=mode, departure_time=now)
16
  if directions_result:
@@ -19,22 +17,61 @@ def get_directions(source, destination):
19
  directions_info[mode] = "No available routes."
20
  return directions_info
21
 
22
- # Streamlit App
23
- st.title("🗺️ Google Maps Directions")
24
- st.sidebar.header('User Input Features')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # Input for source and destination
27
  source_location = st.sidebar.text_input("Source Location", "Mound, MN")
28
  destination_location = st.sidebar.text_input("Destination Location", "Minneapolis, MN")
29
 
30
  if st.sidebar.button('Get Directions'):
31
  directions_info = get_directions(source_location, destination_location)
32
-
33
- # Displaying the directions
34
  for mode, directions in directions_info.items():
35
- st.write(f"## Directions by {mode.capitalize()}")
36
  if directions == "No available routes.":
37
- st.write(directions)
38
  else:
39
  for i, step in enumerate(directions):
40
- st.write(f"{i+1}. {step['html_instructions']}")
 
 
 
 
 
1
+ gmaps = googlemaps.Client(key='AIzaSyDybq2mxujekZVivmr03Y5-GGHXesn4TLI')
2
  import streamlit as st
3
+ import streamlit.components.v1 as components
4
+ import googlemaps
5
+ import os
6
  from datetime import datetime
7
 
 
 
 
 
8
  def get_directions(source, destination):
9
  now = datetime.now()
 
 
10
  directions_info = {}
11
+ modes = ['driving', 'walking', 'bicycling', 'transit']
12
  for mode in modes:
13
  directions_result = gmaps.directions(source, destination, mode=mode, departure_time=now)
14
  if directions_result:
 
17
  directions_info[mode] = "No available routes."
18
  return directions_info
19
 
20
+ def show_map(source, destination):
21
+ html_code = f"""
22
+ <html>
23
+ <head>
24
+ <script src="https://maps.googleapis.com/maps/api/js?key={os.getenv('GOOGLE_KEY')}&callback=initMap" async defer></script>
25
+ <script>
26
+ var map;
27
+ function initMap() {{
28
+ var directionsService = new google.maps.DirectionsService();
29
+ var directionsRenderer = new google.maps.DirectionsRenderer();
30
+ map = new google.maps.Map(document.getElementById('map'), {{
31
+ zoom: 7,
32
+ center: {{lat: 41.85, lng: -87.65}}
33
+ }});
34
+ directionsRenderer.setMap(map);
35
+ var request = {{
36
+ origin: '{source}',
37
+ destination: '{destination}',
38
+ travelMode: 'DRIVING'
39
+ }};
40
+ directionsService.route(request, function(result, status) {{
41
+ if (status == 'OK') {{
42
+ directionsRenderer.setDirections(result);
43
+ }}
44
+ }});
45
+ }}
46
+ </script>
47
+ </head>
48
+ <body onload="initMap()">
49
+ <div id="map" style="height: 500px;"></div>
50
+ </body>
51
+ </html>
52
+ """
53
+ components.html(html_code, height=600, scrolling=False)
54
+
55
+ # Initialize Google Maps
56
+ gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY'))
57
+
58
+ # Streamlit app
59
+ st.title('🗺️ Google Maps Directions')
60
+ st.sidebar.header('📌 User Input Features')
61
 
 
62
  source_location = st.sidebar.text_input("Source Location", "Mound, MN")
63
  destination_location = st.sidebar.text_input("Destination Location", "Minneapolis, MN")
64
 
65
  if st.sidebar.button('Get Directions'):
66
  directions_info = get_directions(source_location, destination_location)
 
 
67
  for mode, directions in directions_info.items():
68
+ st.write(f"## Directions by {mode.capitalize()} 🛣️")
69
  if directions == "No available routes.":
70
+ st.write("❌ " + directions)
71
  else:
72
  for i, step in enumerate(directions):
73
+ st.write(f"👣 {i + 1}. {step['html_instructions']}")
74
+
75
+ show_map_button = st.button('Show Directions on Map 🗺️')
76
+ if show_map_button:
77
+ show_map(source_location, destination_location)