Streamlit is an open-source app framework for Machine Learning and Data Science projects. It's a powerful tool to create web applications with minimal code. In this guide, we will build a weather forecasting web app using Streamlit and a weather API to fetch data.
- Basic understanding of Python
- Knowledge of using APIs
- Streamlit library installed (
pip install streamlit
) - Access to a weather API (like OpenWeatherMap or Weatherstack)
- Temperature: The degree of hotness or coldness measured on a definite scale.
- Humidity: The amount of water vapor in the air.
- Wind Speed: The speed at which the wind is blowing.
- Pressure: The force exerted by the atmosphere at a given point.
- Precipitation: Any form of water - liquid or solid - falling from the sky, including rain, snow, sleet, and hail.
- Visibility: The distance one can see as determined by light and weather conditions.
- Weather Condition: Describes the state of the atmosphere, such as clear, cloudy, rainy, or snowy.
First, install Streamlit and requests libraries using pip:
pip install streamlit requests
import streamlit as st
import requests
your logic
Save the script as weather_app.py
and run it using Streamlit:
streamlit run weather_app.py
- Setting Up: Import Streamlit and requests libraries.
- Main Function:
main()
handles the app's main logic, displaying the title, text input for city name, and a button to fetch weather data. - API Call:
get_weather()
function makes a GET request to the weather API with the provided city name and API key. It returns the weather data in JSON format if the request is successful. - Displaying Data:
display_weather()
function takes the JSON response and extracts relevant information (temperature, humidity, etc.), displaying it in a readable format using Streamlit.
By following these steps, you will have a functional weather forecasting web app using Streamlit. This app can be further enhanced by adding features like a 5-day forecast, charts for visualizing weather data, and user authentication for personalizing the experience.