API reference¶
Wttr ¶
Wttr(
*,
base_url: AnyHttpUrl = build(
scheme="https", host="wttr.in"
),
session: Client | None = None,
)
Client for the wttr.in weather API.
Use it as a context manager or call close when done.
Parameters:
-
base_url(AnyHttpUrl, default:build(scheme='https', host='wttr.in')) –Server to send requests to, e.g. a self-hosted wttr.in.
-
session(Client | None, default:None) –HTTP client to send requests with. If
None, one is created on the first request, with a 60 s timeout (5 s to connect).closecloses it either way.
Examples:
import pywttr
with pywttr.Wttr() as wttr:
weather = wttr.weather("Paris")
weather ¶
Get the current weather and a forecast for 3 days.
Raises httpx2.HTTPStatusError if wttr.in responds with an error status, e.g. for an unknown location.
Parameters:
-
location(str) –City, airport code,
~landmark,latitude,longitudeor anything else wttr.in accepts. -
language(Language, default:EN) –Language of the weather description. It also selects the return type, e.g.
Language.RUgivespywttr.models.ru.Model.
Examples:
import pywttr
with pywttr.Wttr() as wttr:
weather = wttr.weather("Paris", language=pywttr.Language.DE)
print(weather.current_condition[0].lang_de[0].value)
Language¶
Bases: StrEnum
Language of the weather description; values are wttr.in codes.
Examples:
Prefer the member: type checkers then know the exact response model.
language = Language.ZH_CN
language = Language["ZH_CN"]
language = Language("zh-cn")
Response models¶
Each language has its own module, such as pywttr.models.fr. Its CurrentConditionItem and HourlyItem add the translated description twice: as lang_fr and as lang_xx, which every language has.
Response models shared by all languages.
Model ¶
Bases: FrozenModel, Generic[_TCurrentConditionItem, _THourlyItem]
Weather at a location: now and for 3 days.
current_condition ¶
current_condition: tuple[_TCurrentConditionItem, ...]
Current weather, one item.
nearest_area ¶
nearest_area: tuple[NearestAreaItem, ...]
The area nearest to the location, one item.
weather ¶
weather: tuple[WeatherItem[_THourlyItem], ...]
Forecast for today, tomorrow and the day after.
CurrentConditionItem ¶
Bases: FrozenModel
Current weather.
WeatherItem ¶
HourlyItem ¶
Bases: FrozenModel
Forecast for a 3-hour slot.
AstronomyItem ¶
Bases: FrozenModel
Sun and moon for a day.
NearestAreaItem ¶
Bases: FrozenModel
The area nearest to the requested location.
weather_url ¶
weather_url: tuple[WeatherUrlItem, ...]
Forecast page on WorldWeatherOnline, one item.