-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmars_flaskapp.py
More file actions
34 lines (24 loc) · 879 Bytes
/
Copy pathmars_flaskapp.py
File metadata and controls
34 lines (24 loc) · 879 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
33
34
from flask import Flask, render_template, redirect
from flask_pymongo import PyMongo
import scrape_mars
# Create an instance of Flask
app = Flask(__name__)
# Use flask_pymongo to set up mongo connection
app.config["MONGO_URI"] = "mongodb://localhost:27017/mars_app"
mongo = PyMongo(app)
# Route to render index.html template using data from Mongo
@app.route("/")
def index():
# Find one record of data from the mongo database
mars_dict = mongo.db.mars_dict.find_one()
# Return template and data
return render_template("index.html", mars=mars_dict)
@app.route("/scrape")
def scrape():
mars_dict = mongo.db.mars_dict
mars_data = scrape_mars.scrape()
# Update the Mongo database using update and upsert=True
mars_dict.update({}, mars_data, upsert=True)
return redirect("/", code=302)
if __name__ == "__main__":
app.run(debug=True)