0

So I am just learning Python as a "hobby" and am trying to make a gag chatbot site based on a friend who has a, umm, unusual way of speaking. I came up with the below Python code that seems to do the job, but I have no idea how to put this into an actual site that I can share with our group of friends. I have perused around and this question may be too simple for this site, but here it is:

import random
dude_responses = ["Obscenity1", "Obscenity2", "Obscenity3"]
print("Dude: Hello")
input("You: ")
def dude():
    print("RB: " + random.choice(dude_responses))
while True:
    dude()
    input("You: ")

This gets me the below interaction with the Python prompt - how can I mirror this more or less exactly to a simple website? And yes the entire point of the bot is that its responses have no connection to what's being said :)

Dude: Hello You: Hi RB: Obscenity3 You: What? RB: Obscenity1 You: Huh? RB: Obscenity1 You:

Brython
  • 9
  • 2
  • Perhaps you should follow a tutorial to get a website up first. For python, you could look at Django, Flask, or Tornado. (There are many more. These are the ones I am familiar with, and can recommend). Flask is probably the easiest to get started with imo. Then start looking into websockets, perhaps Socket IO. If you follow this you will see where your chatbot can fit. If you want a pre-made solution, you could also make a chatbot in Slack. – Geoff Lentsch Jul 13 '18 at 01:46
  • For something a little easier than websockets or Socket IO, you might try using a simple form (get or post) to send the text to your server and retrieve the response. You might also consider [Meteor](http://meteor.com) for building an interactive site, but then you’d probably be better off using JavaScript instead of Python. – Matthias Fripp Jul 13 '18 at 03:35

1 Answers1

1

There are a few directions you could go with this.

The obvious one, springing from your question title, would be to run your Python code on a web server and then generate web pages that interact with your core code. Those are the types of suggestions you got in the comments on your question. The problem with this approach is that you have to deal with the whole web stack for an interactive client-server application, and those stacks are complicated and heterogenous, so they're hard for beginners to setup. (Server receives request, sends it to server-side app, uses results to generate webpage and session information, sends webpage to client; client renders webpage with controls, then requests additional information either via a form [fairly easy but clunky] or via a websocket or Socket.io [dynamic but even more complicated].) See the comments you got for some pointers in this direction.

An easier option for a simple app like this would be just to do everything in a self-contained page in the web browser. In that case, you'd just need to write some javascript that does the equivalent of your Python app, using controls and buttons instead of input statements. Then embed that javascript in an HTML file, and host the HTML file on any server (possibly even a Google Sites page), or just send the HTML file to your users and let them open it in a web browser. But that wouldn't reuse much of what you've learned about Python already.

Another option would be to use Python in the web browser. This would mean using a library that allows use of Python code directly in a webpage, possibly via WebAssembly. Emscripten and Skulpt seem to be among the more mature implementations of this. Here are some links to interesting discussions of the options for running Python in a browser: How to choose a package for Python in the browser, Compiling Python to WebAssembly, discussion on ycombinator of compiling to WebAssembly, and another discussion in the data science context.

If you want to reuse your Python code and don't care too much about having total control of the hosting environment, your best option would probably be to use a ready-made Python-in-browser hosting environment, like trinket.io (based on Skulpt). This lets you write Python scripts, which they host for you so anyone can run them in a web browser. e.g., here's an implementation of your script (click the pencil icon to edit it). There's also a pretty enthusiastic article about Trinket in Wired.

Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
  • Thanks a lot everyone - the trinket solution is the one I can handle right now, the rest are beyond my current ability level but I have every intention of getting there. Much thanks! – Brython Jul 13 '18 at 22:13