Replies: 4 comments 4 replies
-
Like what part of the framework, could you please elaborate? |
Beta Was this translation helpful? Give feedback.
2 replies
-
i can use async like this; Async Thingsfrom aiohttp import ClientSession
from asyncio import new_event_loop, run
from atexit import register as to_exit
class WeatherData:
def __close_session(self):
run(self.session.close())
def __init__(self, city:str):
self.session = ClientSession()
to_exit(self.__close_session)
self.__api_key = "2a7db0585e7541018229c17efb2efa94"
self.city = city
async def get_data(self):
async with self.session.get(
url = "https://api.weatherbit.io/v2.0/current/weather",
params = {
"city" : self.city,
"key" : self.__api_key
},
) as response:
return await response.json()
async def get_weather(city:str):
weather_api = WeatherData(city)
data = (await weather_api.get_data())["data"][0]
return f"{data['ob_time']} » [{data['country_code']}] {data['city_name']} - {data['weather']['description']}" Flet Thingsfrom flet import app as flet
from flet.page import Page, ControlEvent
from flet import (
ElevatedButton, Text, TextField, icons, ProgressBar, ProgressRing, Row, Switch
)
def main_page(page:Page):
page.title = "Async OpenWeather"
page.horizontal_alignment = "center"
def change_theme_func(_:ControlEvent):
page.theme_mode = "dark" if page.theme_mode == "light" else "light"
change_theme.label = "Light" if page.theme_mode == "light" else "Dark"
page.update()
page.theme_mode = "dark"
change_theme = Switch(label="Dark", on_change=change_theme_func)
def search_func(_:ControlEvent):
if not search_field.value:
search_field.error_text = "Please Enter City!"
return page.update()
page.splash = ProgressBar()
search_btn.disabled = True
page.add(loading)
page.update()
search_value = search_field.value
search_data = new_event_loop().run_until_complete(get_weather(search_value))
search_row = Row([Text(search_data)], alignment="start")
page.splash = None
search_btn.disabled = False
page.remove(loading)
page.update()
page.add(search_row)
search_field = TextField(label="Enter the City Name", width=300, tooltip="eg.: Seattle", hint_text="Ankara, Turkiye")
search_btn = ElevatedButton("Search", icon=icons.SEARCH, on_click=search_func)
loading = Row([ProgressRing()], alignment="start")
page.add(Row([Row([search_field, search_btn]), change_theme], alignment="spaceBetween"))
flet(target=main_page) Magicsearch_data = new_event_loop().run_until_complete(get_weather(search_value)) |
Beta Was this translation helpful? Give feedback.
0 replies
-
So, this |
Beta Was this translation helpful? Give feedback.
1 reply
-
I see. Will dig into that, to see how to support asyncio in Flet. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
it would be great if it had async support
Beta Was this translation helpful? Give feedback.
All reactions