File size: 2,286 Bytes
c20f66f
 
 
 
 
abb2e36
 
 
 
 
c20f66f
 
 
 
 
 
 
 
 
abb2e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f66f
abb2e36
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import altair as alt
import numpy as np
import pandas as pd
import streamlit as st

from dotenv import load_dotenv
import os
import requests


"""
# Welcome to Streamlit!

Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
forums](https://discuss.streamlit.io).

In the meantime, below is an example of what you can do with just a few lines of code:
"""
load_dotenv()
API_KEY = os.getenv("API_KEY")

url = f"https://api.openweathermap.org/data/2.5/weather"
if "cities" not in st.session_state:
    st.session_state.cities = []
st.title("Weather App PRO")
city_input = st.text_input("Find City Weather", "Pyongyang")
if st.button("Add Expander"):
    if city_input: # Only add if the input is not empty
        st.session_state.cities.append(city_input)
        st.success("Expander added!")
    else:
        st.warning("Please enter some content.")

for idx, city in enumerate(st.session_state.cities):
    with st.expander(f"Weather from {city}"):
        params = {
            "q": city,
            "appid": API_KEY,
            "units": "metric"  # temperature in Celsius
        }
        response = requests.get(url, params)
        if response.status_code !=200:
            st.write(F"{city} not found in API")
        data = response.json()
        for key, value in data['main'].items():
            st.write(f"{key}: {value}")
        # st.write(f"Temperature: {data['main']['temp']} degrees Celsius")
        # st.write(f"Humidity: {data['main']['humidity']}%")
        st.write(f"Description: {data['weather'][0]['description']}")
        
# st.session_state.update(st.session_state.cities.append(city_input))
# # for i, city in enumerate(st.session_state.cities):

# # Initialize session state to store expanders
# if "expanders" not in st.session_state:
#     st.session_state.expanders = []

# Text input
# new_text = st.text_input("Enter text to create an expander:")

# # Button to add expander
# if st.button("Add Expander") and new_text:
#     st.session_state.expanders.append(new_text)

# # Display expanders
# for idx, text in enumerate(st.session_state.expanders):
#     with st.expander(f"Expander {idx+1}"):
#         st.write(text)