resource_alloc / app.py
Mayureshd's picture
Update app.py
b517a7e verified
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
import pandas as pd
import streamlit as st
# Fetch the credential JSON file from your Firebase project settings
# and upload it to Google Colab, then provide the path here
cred = credentials.Certificate("cid1234-firebase-adminsdk-o0jz2-ddc0afe71d.json")
# Initialize the app with the credentials and your database URL
# firebase_admin.initialize_app(cred, {
# 'databaseURL': 'https://cid1234-default-rtdb.firebaseio.com/'
# })
# Reference to your Firebase database
ref = db.reference('/')
# Listen for changes in the database in real-time
def on_change(event):
st.write('Data changed:', event.path, event.data)
ref.listen(on_change)
import requests
def get_coordinates(location):
"""
Get latitude and longitude of a location using OpenStreetMap Nominatim API
"""
# URL for OpenStreetMap Nominatim API
url = f'https://nominatim.openstreetmap.org/search?format=json&q={location}'
# Send request to the API
response = requests.get(url)
# Parse JSON response
data = response.json()
# Check if response is not empty
if data:
# Extract latitude and longitude from the first result
lat = float(data[0]['lat'])
lon = float(data[0]['lon'])
return lat, lon
else:
st.write("No results found for the location.")
return None
# Example usage
# location = input("Enter Location: ")
# latitude, longitude = get_coordinates(location)
# if latitude and longitude:
# st.write(f"Latitude: {latitude}, Longitude: {longitude}")
# else:
# st.write("Failed to retrieve coordinates for the location.")
crimes = pd.DataFrame(columns=['Name', 'Lat', 'Lon', 'Intensity'])
query = ""
while(query != 'N'):
location = st.text_area("Enter Location: ")
crime = st.text_area("Enter Crime: ")
intensity = int(st.text_area("Enter Intensity: "))
latitude, longitude = get_coordinates(location)
data = [{'Name': crime, 'Lat': latitude, 'Lon': longitude, 'Intensity': intensity}]
query = st.text_area("Enter Y to continue and N to stop: ")
crimes = pd.concat([crimes, pd.DataFrame(data)], ignore_index=True)
st.write(crimes)