N
N
NEW MAROLUC2021-10-22 21:03:25
Python
NEW MAROLUC, 2021-10-22 21:03:25

I made an online chat in Python and pywebio, the "Back to chat" button does not work, what should I do?

Good evening. I made an online chat on this lesson: Link . Everything works except... when you leave the chat, the Back to chat button doesn't work, it should return to the chat, it doesn't work
Here's the code:

from pywebio import start_server
from pywebio.input import *
from pywebio.output import *
from pywebio.session import run_async, run_js

import asyncio


chat_msgs = []
online_users = set()

MAX_MESSAGES_COUNT = 100

async def main():
  global chat_msgs

  put_markdown("##  Welcome to online chat")


  msg_box = output()
  put_scrollable(msg_box, height=300, keep_bottom=True)

  nickname = await input("Log in chat", required=True, placeholder="Your name", validate=lambda n: "This nickname is already in use" if n in online_users or n == '' else None)
  online_users.add(nickname)

  chat_msgs.append(('', f"`{nickname}` joined the chat!"))
  msg_box.append(put_markdown(f"`{nickname}` joined the chat!"))

  refresh_task = run_async(refresh_msg(nickname, msg_box))

  while True:
    data = await input_group(" New message", [
      input(placeholder="Message text", name="msg"),
      actions(name="cmd", buttons=["Send", {'label':"Leave the chat", 'type':'cancel'}])
    ], validate=lambda m: ('msg', "Enter your message!") if m["cmd"] == "Send" and not m["msg"] else None)

    if data is None:
      break

    msg_box.append(put_markdown(f"`{nickname}`: {data['msg']}"))
    chat_msgs.append((nickname, data['msg']))

  # exit chat
  refresh_task.close()

  online_users.remove(nickname)
  toast("You left chat!")
  msg_box.append(put_markdown(f" User `{nickname}` leave the chat!"))
  chat_msgs.append(('', f"User `{nickname}` leave the chat!"))

  *****вот тут проблема*****put_buttons(["Back to chat"], onclick=lambda btn: run_js('window.location.reload('))

async def refresh_msg(nickname, msg_box):
  global chat_msgs
  last_idx = len(chat_msgs)

  while True:
    await asyncio.sleep(1)

    for m in chat_msgs[last_idx:]:
      if m[0] != nickname:
        msg_box.append(put_markdown(f"`{m[0]}`: {m[1]}"))

    # remove expired
    if len(chat_msgs) > MAX_MESSAGES_COUNT:
      chat_msgs = chat_msgs[len(chat_msgs) // 2:]
        
    last_idx = len(chat_msgs)

if __name__ == "__main__":
  start_server(main, debug=True, port=8080, cdn=False)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
psaly2015, 2021-10-23
@newmaroluc

You have a quote in the wrong place, as a result, the page reload function looks like this
window.location.reload(, but it should be window.location.reload()
As a result, the correct string will be

put_buttons(['Back to chat'], onclick=lambda btn:run_js('window.location.reload()'))

A
AndrewPrist, 2022-01-07
@AndrewPrist

HOW DID YOU INSTALL THIS PAIVEWEB!? WHEN I RUN A SIMPLE COMMAND I WRITE THAT THE MODULE IS NOT FOUND ALTHOUGH I HAVE THE LATEST VERSION OF PYTHON, PIPA AND PAYWEB I INSTALLED FROM PIPA AND FROM THE OFFICIAL SITE
from pywebio import start_server
from pywebio.input import *
from pywebio.outputio import.ses * from pywebio.outputio import *
from pywebio.outputio import.ses import run_js
async def main():
put.text("hi")
if __name__ == "__pywebio__":
start_server(main, debug=True, port=9096, cdn=False)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question