-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
41 lines (29 loc) · 1.08 KB
/
app.py
File metadata and controls
41 lines (29 loc) · 1.08 KB
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
# Dependencies
from flask import Flask, render_template, redirect
import pymongo
import scrape_mars
# create instance of Flask app
app = Flask(__name__)
# we still meed to tell the library where the database is and provde a connection string to Mongo
conn = 'mongodb://localhost:27017'
client = pymongo.MongoClient(conn)
# Connect to the database scraped_mars. it will create it if not available
db = client.scraped_mars
#collection in the database
collection = db.mars
# Set route to query the mongo database and find all the items in the collections.Set it to a variable.
#pass the mars data into an HTML template to display the data.
@app.route('/')
def index():
data_mars = list( collection.find() )
print(data_mars)
return render_template('index.html', data_mars=data_mars)
@app.route('/scrape')
def scrape():
collection = db.mars
#import your scrape_mars.py script and call your scrape function.
mars_data = scrape_mars.scrape()
collection.update({},mars_data,upsert=True)
return redirect("/", code=302)
if __name__ == "__main__":
app.run(debug=True)