Question:
I’m new to therading btw:
How can I make the threading in this code better? I want to make the inputs not get erased on refresh but still be there when enter is pressed. I also want the refreshing to happen only when a new message is added and I dont want refreshing to lag. Also feel free to tell me if there is any other way to make the code better.
Repl link:
https://replit.com/@MiloCat/Chatting-Oversimplified#main.py
import os
import time
import threading
from replit import db
db.db_url ="https://kv.replit.com/v0/eyJhbGciOiJIUzUxMiIsImlzcyI6ImNvbm1hbiIsImtpZCI6InByb2Q6MSIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjb25tYW4iLCJleHAiOjE2NzUxMzQ5MzIsImlhdCI6MTY3NTAyMzMzMiwiZGF0YWJhc2VfaWQiOiI2NWFkYzVhNS1mMDE3LTRjNGUtYjkwOC1lODdhYjgxYTliNmEiLCJ1c2VyIjoiTWlsb0NhdCIsInNsdWciOiJDaGF0dGluZy1PdmVyc2ltcGxpcensored.xxxxxxx4ye1VjDayALaku1-E6vxPpOIZO70Fow5x_GsaA9Wa_wS13y_BfBiG471qM84kqxbNf_2BqAJs1hlY93rftK5aQ"
user = os.environ["REPL_OWNER"]
if user in ("MiloCat", "ItsRaphael"):
checkmark = "✓"
else:
checkmark = ""
print("[1] Chat\n[2] Leave")
start = input("")
def update_chat():
while True:
last_x_messages = db["messages"][-10:] # Only prints the last 10 messages
print(*last_x_messages, sep='\n')
time.sleep(1)
print("\033c", flush=True, end="")
if start == "2":
thread = threading.Thread(target=update_chat)
thread.start()
while True:
mess = input(f"{checkmark}{user}: ")
message = f"{checkmark}{user}: {mess}"
db["messages"].append(message)
Edit: