I am working on a small project in Python. It is divided into two parts.
The first part is responsible for crawling the web page and extracting some information and inserting it into the database.
The second part is applicable for presenting this information using a database. Both parts share a database. In the second part, I use a flash framework to display html information with some formatting, style, etc., to make it cleaner.
The source files of both parts are in the same package, but for this program to work correctly, the user must run the scanner and the recipient of the results separately, for example:
python crawler.py
and then
python presenter.py
Everything is in order, except for one. What should I do to create the result in html format and open the results page in the default browser, but it always opens twice, probably due to the presence of the run () method, which launches Flask in a new thread and things become muddy for me . I do not know what I have to do to make my presenter .py open only one tab / window after it starts.
Here is a snippet of my code:
from flask import Flask, render_template import os import sqlite3 # configuration DEBUG = True DATABASE = os.getcwd() + '/database/database.db' app = Flask(__name__) app.config.from_object(__name__) app.config.from_envvar('CRAWLER_SETTINGS', silent=True) def connect_db(): """Returns a new connection to the database.""" try: conn = sqlite3.connect(app.config['DATABASE']) return conn except sqlite3.Error: print 'Unable to connect to the database' return False @app.route('/') def show_entries(): u"""Loads pages information and emails from the database and inserts results into show_entires template. If there is a database problem returns error page. """ conn = connect_db() if conn: try: cur = connect_db().cursor() results = cur.execute('SELECT url, title, doctype, pagesize FROM pages') pages = [dict(url=row[0], title=row[1].encode('utf-8'), pageType=row[2], pageSize=row[3]) for row in results.fetchall()] results = cur.execute('SELECT url, email from emails') emails = {} for row in results.fetchall(): emails.setdefault(row[0], []).append(row[1]) return render_template('show_entries.html', pages=pages, emails=emails) except sqlite3.Error, e: print ' Exception message %s ' % e print 'Could not load data from the database!' return render_template('show_error_page.html') else: return render_template('show_error_page.html') if __name__ == '__main__': url = 'http://127.0.0.1:5000' webbrowser.open_new(url) app.run()
python flask
koleS
source share