Skip to content

Basic echo chat web app in Python

Have you ever heard an echo in the mountains, a cave, or a tunnel? An echo is a simple response: it repeats your voice back to you.

In this tutorial, we will build a simple Echo Chat Bot. It will respond with the same text that the user typed.

You will learn how to use these Mercury widgets:

  • Chat (/docs/chat/chat/)
  • ChatInput (/docs/chat/chatinput/)
  • Message (/docs/chat/message/)

Below is the screenshot of echo bot developed in Python notebook. In the left there is notebook code, in the right there is live app preview:

Echo Chat Web App created in Python

The live web app:

🚀 Load interactive demo Hover to start

In the first notebook cell, import Mercury:

import mercury as mr

We will use mr alias to create all widgets.

In the next cell, create a place where all messages will be displayed:

chat = mr.Chat()

Yes, it is that simple :)

Now we need ChatInput to let the user type a message:

prompt = mr.ChatInput()

When user enters the text and send it, this automatically re-execute cells below.

In the next cell, we will:

  1. Create a message from the user input
  2. Add it to the chat
  3. Create the assistant response (Echo: ...)
  4. Add it to the chat
if prompt.value:
# user message
user_msg = mr.Message(prompt.value, role="user")
chat.add(user_msg)
# assistant message (echo)
response_msg = mr.Message(
f"Echo: {prompt.value}",
role="assistant",
emoji="🤖",
)
chat.add(response_msg)
  • prompt.value contains the latest user input.
  • mr.Message(..., role="user") creates a message from the user.
  • mr.Message(..., role="assistant") creates a message from the bot.
  • chat.add(...) appends messages to the chat view.

This echo bot is a great starting point. Next you can:

  • Replace the echo response with an LLM call (OpenAI, Ollama, OpenRouter, Anthropic, Mistral, Bielik, etc.).
  • Add system prompts and tools for an agent-like chat.
  • Build interactive responses with charts and tables.