Skip to content

Instantly share code, notes, and snippets.

@yoniLavi
Created July 6, 2017 12:31
Show Gist options
  • Select an option

  • Save yoniLavi/3c6ae93b9bee7899101f0c670ff2ad2d to your computer and use it in GitHub Desktop.

Select an option

Save yoniLavi/3c6ae93b9bee7899101f0c670ff2ad2d to your computer and use it in GitHub Desktop.
"""A very basic url-based chat"""
from flask import Flask, redirect
app = Flask(__name__) # the flask application object
messages = ['System: Hi everyone'] # a global object for all chat messages
def all_messages():
"""Helper function to get a string of all chat messages, one per line"""
return '<br>'.join(messages)
@app.route('/')
def hello():
"""Main page, with instructions"""
return 'Hi, chat by getting the resource /chat/USERNAME/MESSAGE'
@app.route('/chat/<username>')
def view_chat(username):
"""Chat view page, personalised for the user"""
return '<h1>Welcome, {}</h1>{}'.format(username, all_messages())
@app.route('/chat/<username>/<message>')
def new_message(username, message=None):
"""A special page used to post new chat messages; redirects to chat"""
messages.append("{}: {}".format(username, message))
return redirect('/chat/{}'.format(username))
# Run the server with debug mode enabled, to reload when code changes
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment