Problems with py script, pyodide, API and queries

79 views Asked by At

I'm doing a project, I need to print a weather forecast, it doesn't seem to give out an error code, but the page is empty. Nothing is output your text

<!DOCTYPE html>
<html>
    <head>
        <script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
        <link rel="stylesheet" href="https://pyscript.net/releases/2022.12.1/pyscript.css"/>
    </head>
    <body>
        <py-script>
            import pyodide
            import asyncio
            API_KEY = '8e4cf478-53b6-45bc-8597-9946db9f1fd5'
            url = 'https://api.weather.yandex.ru/v2/forecast'
            params = {
                'lat': 55.753215,
                'lon': 37.622504,
                'lang': 'ru_RU'
            }
            headers = {
                'X-Yandex-API-Key': API_KEY
            }

            async def get_weather():
                response = await pyodide.fetch(url=url, params=params, headers=headers, method="GET")
                response_dict = await response.json()
                data = response.json()
                fact = data.get('fact')
                temperature = fact.get('temp')
                print(f'Погода в Москве: {temperature} градусов по Цельсию')

            asyncio.ensure_future(get_weather())
        </py-script>
    </body>
</html>
1

There are 1 answers

0
Jeff Glass On

In PyScript 2023.11.1 and newer, print only routes to the browser's developer console. Use PyScript's display() function to send content to the page.