How to Handle Conversation in Chatbot in Python
When you develop a chatbot, sometimes for user experience, you cannot ask your user send messages like commands. For example, we want to build a guess number bot. We want the bot works like this: user: guess bot: From what number? user:: 25 bot: To what number? user: 100 bot: Guess a number between 25 to 100 user: 64 bot: too small user: 91 bot: too large …… user: 83 bot: Correct! You spent 6 times to guess this number. However, the common way we dealing with requests in the backend is one-request-one-response. That would be a disaster to separate a lot of handlers from the conversation....
How to Redirect Stdout to Streaming Response in Django
Sometimes we need to execute some long tasks at the backend, and the tasks are complicated and error-prone. So we hope users can see the real-time console log. Thus we need to redirect the stdout in our functions to the user’s browser. Given a function like the following. How to see the stdout in real-time in the browser? import time def job(times): for i in range(times): print(f'Task #{i}') time.sleep(1) print('Done') time.sleep(0.5) Streaming Response Normally, responses are sent after all the data has been collected. However, sometimes we can’t wait until the data is prepared. In this case, we’ll use streaming...