Create Web Based ChatBot in Python, Django, Flask

Learn how to create Chatbot in Python. I this tutorial, we will use Chatterbot Library for creating the chat bot. We will use Flask Framework for deploying the chatbot on web. This tutorial change be used with Django also.

Chatbot in Python, Flask, Django, Chatterbot

Every Chatbot has a theme. Our chatbot is going to Answer the Questions of User of Coronavirus Disease. It will greet the user, interact with them and give answers of Covid-19.

Installing Chatbot Required Libraries

All the libraries are installed in Virtualenv.

pip install Flask
pip install chatterbot
pip install chatterbot-corpus 

Create Chatbot in Python

Now we have ca chatterbot, we will create a file and paste the bellow code.

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainer

# Creating ChatBot Instance
chatbot = ChatBot('CoronaBot')

 # Training with Personal Ques & Ans 
conversation = [
    "Hi there!",
    "How are you doing?",
    "I'm doing great.",
    "That is good to hear",
    "Thank you.",
    "You're welcome."

trainer = ListTrainer(chatbot)

# Training with English Corpus Data 
trainer_corpus = ChatterBotCorpusTrainer(chatbot)

In the above code, we are creating a chatterbot instance. Then we are training our chatbot with ListTrainer with our personal question and answers. Then we are using chatterbot corpus english data to train our chatbot.

Creating Web UI for Chatbot using Flask

As we have installed Flask, we will create a file and paste the bellow code inside it.

from chatbot import chatbot
from flask import Flask, render_template, request

app = Flask(__name__)
app.static_folder = 'static'

def home():
    return render_template("index.html")

def get_bot_response():
    userText = request.args.get('msg')
    return str(chatbot.get_response(userText))

if __name__ == "__main__": 

In the above code, we are creating routes for our web application. In get_bot_response() we are taking input from html form and after processing chatbot giving response.

HTML Template for ChatBot

Create a folder templates and add this index.html inside it – Download.

Create a folder static, inside it create styles folder and inside it add this file style.cssDownload

The above file path should match like bellow –


In index.html file, we have written a script for sending the data which is input to chatbot form and take the response to append it to chatbot window.

Running Chatbot Project

Once the you have setup the project, we need to just run the flask app.


If everything goes right, go to – http://localhost:5000/ and enjoy your own personal chatbot.

Note – If you see not see right answer for question, delete the .sqlite3 database file from your folder.

Optimizing and Adding Feature to Chatbot

We have created chatbot successfully, but it as basic example. We need to add more features to our chatbot.

Open your file and do some changes like


chatbot = ChatBot('CoronaBot',) 


chatbot = ChatBot(
            'import_path': 'chatterbot.logic.BestMatch',
            'default_response': 'I am sorry, but I do not understand. I am still learning.',
            'maximum_similarity_threshold': 0.90

In above replacing, we have added default answer, database and added logic adapter which can answer maths and time questions.

Using file for question and answer

In the above, we have used conversation list. Now we will replace with it a file where question and answer are written inside it are loaded and fed to ListTrainer.


 # Training with Personal Ques & Ans 
conversation = [
    "Hi there!",
    "How are you doing?",
    "I'm doing great.",
    "That is good to hear",
    "Thank you.",
    "You're welcome."

trainer = ListTrainer(chatbot)


training_data_quesans = open('training_data/ques_ans.txt').read().splitlines()
training_data_personal = open('training_data/personal_ques.txt').read().splitlines()

training_data = training_data_quesans + training_data_personal

trainer = ListTrainer(chatbot)

We are loading data form training_data/ques_ans.txt and training_data/personal_ques.txt. You can download those files from here.

Project Github


Andrew Murdoch

Hallo Huzaif Sayyed,

I was really interested in creatiing the Chatbot following your instruction. Unfortunately it failed for me. I could not install the Chatterbox. Could it be that I have to use a 64bit version of Python. I am using Python v 3.7.3 (32bit)?

Could you please give advice about what versions you use to create the Chatbot?

Many thanks


Huzaif Sayyed

Are you using VirtualEnv, Because it is very important. It won’t mess up with your core python code.


i cannot install chatterbot-corpus in linux machine
i am getting error no matching distribution found


debes usar la versión de 64 bits de Python? de lo contrario te saldra error.

Andrew Murdoch

Dear Huzaif,

Yes I was using the VENV virtual environment – and 32 bit version of Python…on a Windows PC.

I want to install Chatterbot on a Raspberry Pi – but first of all to get it working I would install on a set-up that is known to work.

Could you advise what platform you are using – and what versions of OS and Python – this would really be helpful.

Many thanks


Ram Sankar

i have created this one in django,but i cannot get answer to my question
eg: hi
Not Found: /get
"GET /get?msg=hi HTTP/1.1" 404
please say what can i do???


Hi, I keep getting this message
"GET /favicon.ico HTTP/1.1" 404 –

I placed an icon as well in the same name in the same place as that of, still I am getting this error.

How to make this chat retrieve data from DB instead of just replying Q&A based chat?


What changes should be made in views and urls files in django??


Added this in the HEAD section and the /favicon.ico error went off

<link rel="icon" href="data:,">

Aswhani Pareek

Error in installing chatterbot in vscode windows


Please add steps for integrating this script in django


Hello can you plz help me change the background image in the index.html file


hello sir,chatterbot cannot install install within the environment?please help!!

Waqas Ahmad

I have setup the project, when I run python, but i didn’t found any app created it shows blank web page

suprobhat ganguly

thanks sir ChatBot python code are working good…my concept is clear know


how to open youtube or google from typing in chat bot..
you : open youtube
chatbot : youtube is open now
is this possible??


Hello, how to do the same thing in Django. I need to create my project in django implemeting the chatbot.

Chinmayee Devraj Achrekar

sir the file gets compiled successfully but when i open the url http://localhost:5000/ it gives me an Internal Server Error…..plsss sir help


Thanks for the tutorial. I have been able to follow it so far…. The only question I have is how would I now integrate this with a website or an application?

Ayushi Agarwal

Those who have problem while downloading chatterbot ..please install python of 64-bit.

Comments are closed.