import time
from splinter import Browser
from bs4 import BeautifulSoup
import pandas as pd
from selenium import webdriver
from flask import Flask, render_template, jsonify, redirect
from flask_pymongo import PyMongo
import scrape_mars.py

app = Flask(__name__)

mongo = PyMongo(app)

@app.route("/")
def index():
    mars_facts = mongo.db.mars_facts.find_one()
    return render_template("index.html", mars_facts=mars_facts)


@app.route("/scrape")
def scrape():
    mars_facts = mongo.db.mars_facts
    mars_facts_data = scrape_mars.scrape()
    print(mars_facts_data)
    mars_facts.update(
        {},
        mars_facts_data,
        upsert=True
    )
    return redirect("http://localhost:5000/", code=302)


if __name__ == "__main__":
    app.run(debug=True)