Created
April 23, 2019 15:02
-
-
Save davegotz/b554da145eb2172d7ce80c2c6441d69d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = 'David Gotz, gotz@unc.edu, Onyen = gotz' | |
| from flask import Flask | |
| from flask import request | |
| def create_web_app(): | |
| app = Flask(__name__) | |
| # Open tweet data from http://www.trumptwitterarchive.com/archive | |
| trump_tweets = open("trump_tweets.csv","r").readlines() | |
| # Define the web pages. | |
| @app.route("/index.html") | |
| @app.route("/") | |
| def index(): | |
| return """ | |
| <html><body> | |
| <form method='get' action='/search'> | |
| Keywords to search for: <input id="keywords" value="" name="keywords" size='50'/> | |
| <input type='submit' value='Submit' /> | |
| </form></body> | |
| <img src='/static/twitter_logo.png'> | |
| </html>""" | |
| @app.route("/search") | |
| def tips(): | |
| keywords = request.args.get('keywords') | |
| html_result = "<form method='get' action='/search'>" | |
| html_result += "Keywords to search for: <input id='keywords' value='"+keywords+"' name='keywords' size='50'/>" | |
| html_result += "<input type='submit' value='Submit' />" | |
| html_result += "</form><p>" | |
| html_result += "<h3>Search Results</h3><p><ol>" | |
| for tweet in trump_tweets: | |
| if keywords in tweet: | |
| # Add bold tag for keywords | |
| tweet = tweet.replace(keywords, "<b>"+keywords+"</b>") | |
| html_result += "<li>" + tweet | |
| html_result += "</ol>" | |
| return html_result | |
| return app | |
| # Run the web server. | |
| def main(): | |
| app = create_web_app() | |
| app.run(host='0.0.0.0', port=1234) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment