Skip to content

Commit ee00034

Browse files
committed
Add training command to example Django app (for gunthercox#2079)
1 parent a833d40 commit ee00034

7 files changed

Lines changed: 61 additions & 5 deletions

File tree

docs/django/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Begin by making sure that you have installed both ``django`` and ``chatterbot``.
1515

1616
pip install django chatterbot
1717

18-
For more details on installing Django, see the `Django documentation`_.
18+
For more details on installing and using Django, see the `Django documentation`_.
1919

2020
Installed Apps
2121
--------------

docs/django/views.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
=======================
2-
ChatterBot Django Views
3-
=======================
1+
=============================
2+
ChatterBot Django Sample Code
3+
=============================
44

55
.. note::
66

@@ -25,3 +25,16 @@ The endpoint expects a JSON request in the following format:
2525
:caption: examples/django_example/django_example/views.py
2626
:language: python
2727
:pyobject: ChatterBotApiView
28+
29+
30+
Example Django Management Commands
31+
==================================
32+
33+
ChatterBot's Django example includes a management command that
34+
demonstrates a simple example of training. This can be used as
35+
a basis for other custom management commands used with other
36+
:ref:`training options <Training>`.
37+
38+
.. literalinclude:: ../../examples/django_example/django_example/management/commands/train.py
39+
:caption: examples/django_example/django_example/management/commands/train.py
40+
:language: python

examples/django_example/django_example/management/__init__.py

Whitespace-only changes.

examples/django_example/django_example/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
This is an example of a custom Django management command that
3+
trains a ChatterBot instance with specified data.
4+
5+
For more information on how to create custom management commands,
6+
see the Django documentation:
7+
https://docs.djangoproject.com/en/4.2/howto/custom-management-commands/
8+
9+
For details on the available training options for ChatterBot see:
10+
http://docs.chatterbot.us/training/
11+
"""
12+
13+
from django.core.management.base import BaseCommand
14+
from django.conf import settings
15+
16+
from chatterbot import ChatBot
17+
from chatterbot.trainers import ListTrainer
18+
19+
20+
class Command(BaseCommand):
21+
help = 'Train a ChatterBot instance with specified data.'
22+
23+
def handle(self, *args, **options):
24+
chatbot = ChatBot(**settings.CHATTERBOT)
25+
26+
trainer = ListTrainer(chatbot)
27+
28+
trainer.train([
29+
'Hello, how are you?',
30+
'I am good.',
31+
'That is good to hear.',
32+
'I am glad to hear that.',
33+
'Thank you.',
34+
'You are welcome.',
35+
])
36+
37+
self.stdout.write(
38+
self.style.SUCCESS('Training completed successfully')
39+
)

examples/django_example/django_example/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
'django.contrib.staticfiles',
4040

4141
'chatterbot.ext.django_chatterbot',
42+
43+
# Our example app:
44+
'django_example',
4245
]
4346

4447
# ChatterBot settings

examples/django_example/django_example/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from django.views.generic.base import TemplateView
33
from django.views.generic import View
44
from django.http import JsonResponse
5+
from django.conf import settings
6+
57
from chatterbot import ChatBot
6-
from chatterbot.ext.django_chatterbot import settings
78

89

910
class ChatterBotAppView(TemplateView):

0 commit comments

Comments
 (0)