-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_web.py
49 lines (40 loc) · 1.17 KB
/
app_web.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, render_template, redirect
from flask_pymongo import PyMongo
import web_mission_to_mars
from dotenv import load_dotenv
import os
load_dotenv()
password = os.getenv("password")
# Create an instance of Flask
app = Flask(__name__)
# Use PyMongo to establish Mongo connection
mongo = PyMongo(app, uri=f"mongodb+srv://cckuqui:{password}@cluster0.52abc.mongodb.net/mars_data?retryWrites=true&w=majority")
# Route to render index.html template using data from Mongo
@app.route("/")
def home():
mars = mongo.db.mars.find_one()
if mars is None :
mars = {
'ntitle':'',
'nbody':'',
'feat_img':'',
'img_det':'',
'weather':'',
'facts':'',
'h':'',
'date':''
}
return render_template("index.html", mars=mars)
# Route that will trigger the scrape function
@app.route('/scrape/')
def scrape():
mars = mongo.db.mars
mars_data = web_mission_to_mars.mars_scrape()
print(mars_data)
mars.replace_one({}, mars_data, upsert=True)
return redirect("/")
@app.route("/about/")
def about():
return render_template("about.html")
if __name__ == "__main__":
app.run(debug=True)