-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
32 lines (29 loc) · 1007 Bytes
/
app.py
File metadata and controls
32 lines (29 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask, render_template, request, url_for
from pymongo import MongoClient
import socket
#Mongo Settings
client = MongoClient('localhost', 27017)
db = client.test_database
collection = db.test_collection
posts = db.posts
# Initialize the Flask application
app = Flask(__name__)
# Define a route for the default URL, which loads the form
@app.route('/')
def form():
if posts.count()!=0:
N=posts.count()
return render_template('form_submit.html', messages=posts.find().sort('_id')[N-1]['message'],hostname=socket.gethostname())
else:
return render_template('form_submit.html', messages="There are no messages :)",hostname=socket.gethostname())
@app.route('/chat/', methods=['POST'])
def chat():
message=request.form['message']
posts.insert_one(dict(message=message))
return render_template('form_action.html', message=message,hostname=socket.gethostname())
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("5000"),
debug=True,
)