Heuer-Stories

import asyncio from pyodide.http import pyfetch import json password = "" ipPort = 'vps2441966.servdiscount-customer.com:5000' async def post_tweet(): tweet = Element('tweet-input').element.value password = Element('password').element.value tweetTitel = Element('tweet-titel').element.value headers = {'Content-Type': 'application/json'} body = json.dumps({'tweetTitle': tweetTitel,'tweet': tweet, 'password': password}) if tweet and password and tweetTitel: response = await pyfetch(f'https://{ipPort}/post_tweet', method='POST', headers=headers, body=body) asyncio.create_task(fetch_tweets()) Element('tweet-input').element.value = '' Element('tweet-titel').element.value = '' # Add logic to handle the response async def fetch_tweets(): password = Element('password').element.value Element('posts').element.innerHTML = '' headers = {f'Authorization': password} response = await pyfetch(f'https://{ipPort}/get_tweets', method='GET', headers=headers) data = await response.json() Element('password').element.style.display = 'none' Element('post-buttonPW').element.innerHTML = 'Refresh Posts' if password[0] == 'g': Element('send').element.style.display = 'block' Element('posts').element.innerHTML = '' for d in data: d = dict(d) title = d['title'] text = d['text'] WrittenBy = d['by'] new_post = f"
{title}

{text}

Gepostet von {WrittenBy}
" Element('posts').element.innerHTML = new_post + Element('posts').element.innerHTML # Bind the post_tweet function to the button def on_post_click(event): asyncio.create_task(post_tweet()) def on_post_clickPW(event): asyncio.create_task(fetch_tweets()) Element('post-button').element.onclick = on_post_click Element('post-buttonPW').element.onclick = on_post_clickPW # Schedule fetch_tweets to run asynchronously #asyncio.create_task(fetch_tweets())