You are on page 1of 3

Creating a full-fledged chatting app like Facebook would require a lot of code and complex functionality.

However, I can provide you with a basic example of a simple chat app using Python and the Flask
framework.

```python

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

messages = []

@app.route('/')

def index():

return render_template('index.html', messages=messages)

@app.route('/send_message', methods=['POST'])

def send_message():

message = request.form['message']

messages.append(message)

return jsonify({'message': 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.

Here is the corresponding HTML template (index.html):


```html

<!DOCTYPE html>

<html>

<head>

<title>Chat App</title>

</head>

<body>

<h1>Chat App</h1>

<div id="chat">

{% for message in messages %}

<p>{{ message }}</p>

{% endfor %}

</div>

<form id="message-form">

<input type="text" id="message-input">

<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();

var message = $('#message-input').val();

$.post('/send_message', {'message': message}, function(data) {

$('#chat').append('<p>' + data.message + '</p>');

$('#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.

You might also like