Professional Documents
Culture Documents
However, I can provide you with a basic example of a simple chat app using Python and the Flask
framework.
```python
app = Flask(__name__)
messages = []
@app.route('/')
def index():
@app.route('/send_message', methods=['POST'])
def send_message():
message = request.form['message']
messages.append(message)
if __name__ == '__main__':
app.run(debug=True)
```
In this example, we create a basic Flask app with two routes. The index route renders an HTML template
that displays the chat messages, and the send_message route receives new messages from the client
and adds them to the messages list.
<!DOCTYPE html>
<html>
<head>
<title>Chat App</title>
</head>
<body>
<h1>Chat App</h1>
<div id="chat">
{% endfor %}
</div>
<form id="message-form">
<button type="submit">Send</button>
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#message-form').submit(function(event) {
event.preventDefault();
$('#message-input').val('');
});
});
});
</script>
</body>
</html>
```
This HTML template includes a form for sending new messages and uses jQuery to make an AJAX request
to the send_message route when the form is submitted. The new message is then appended to the chat
display.
This is a very basic example, and a real-world chat app would require a lot more functionality, such as
user authentication, real-time updates, and message persistence. However, this should give you a
starting point for building a simple chat app in Python.