I am trying to make a WebApp with Flask which gets an input from a user which are keywords and uses that input to scrape the keywords off another website. I have written the Python script to do this using Selenium. Currently the script runs perfectly fine on my computer and outputs printed strings. It takes ~5 seconds to run (as it opens a Firefox browser).
However, I want to take this a step further by creating a website which allows users to type in the keywords, returning them the outputted strings.
So far I have tried implementing Flask on pythonanywhere.com but am having trouble connecting my script to the WebApp.
What I have so far is the following code (mostly found online and from tutorials):
from flask import Flask
from flask import render_template, abort, jsonify, request,redirect, json
from script_to_run import main
app = Flask(__name__)
app.debug = True
@app.route('/')
def index():
return render_template('index.html')
@app.route('/learning', methods=['POST'])
def learning():
data = json.loads(request.data)
# data == {"userInput": "whatever text you entered"}
response = main(data)
return jsonify(response)
if __name__ == '__main__':
app.run()
This is my main app where script_to_run is the python script i want to import to run.
e.g. it may be the following
def main():
import selenium
#some selenium web scraping code here
return 'successfully returned data'
And finally I have my html code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../static/script.js"></script>
</script>
</head>
<body>
<h1>Calculation</h1>
<h1>Test Page</h1>
<input id="user-input" placeholder="Type keywords here; separate them with a space. e.g. lithium gold trading halt"></input>
<p id="results">Results will go here<p>
<button id="submit">Submit</button>
<script>
$(document).ready(function(){
$("#submit").click(function(event){
var uInput = $("#user-input").val();
$.ajax({
type: "POST",
url: '/learning',
data: JSON.stringify({userInput: uInput}),
contentType: 'application/json',
success: function(response){
$("#results").text(response.results);
},
});
});
});
</script>
</body>
</html>
I have two main questions: 1. Is what I am trying to do possible given that my script takes about 5 seconds to run and requires Firefox 2. How would I go about fixing my current code (I am currently unable to import main from script_to_run so can't even test what I've got properly)
Please note that I am a total beginner. I am okay at python (roughly 9 months learning the basics) and have almost no knowledge of web development (but can intuitively figure out basic html). Thanks!