The view page which has a button that triggers scraping on click and the button logic is in javascript. Now I have bonded the button click to a flask function. The URL contains flask variable (eg. scrape.route('/')). Now I wish to extract this domain value and pass it to the flask function.
#blueprint
def create_blueprint():
scrape = Blueprint('scrape', __name__, url_prefix='/jarvis')
@scrape.route('<domain>/start_scrape')
def start_scrape(domain):
resp = get_links(domain)
return resp
#button logic
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('a#start_button').bind('click', function() {
if(!startScrape()) {
return false;
}
document.getElementById('start_button').style.display = "none";
var url = window.location.href;
var url_splits = url.split('/');
var domain_v = String(url_splits[url_splits.length - 1]);
document.write(domain_v)
$.getJSON({{ url_for('scrape.start_scrape', domain=domain_v)|tojson}}, {
name : $('input[name="scrape_name_input"]').val(),
queries : $('textarea[name="query_input"]').val()
}, function(data) {
endScrape();
console.log(data);
alert(data["message"]);
window.location.reload();
scrape_name_input.value = "";
});
return false;
});
});
I tied to get the domain by splitting the url and extract the last part but when I try to build the url using url_for it behaves weirdly. Url for requires the domain variable while building the URL and it works if I hardcode a value e.g. 'bing' but if I use a variable as mentioned in above code it fails and the endpoint url becomes:
GET /jarvis//start_scrape
I don't know why it happens. I require to get the domain dynamically so I can't hardcode it. I referred to following but no help: Flaskr url_for with parameters not working when called from javascript to python, Pass argument to flask from javascript, Pass JavaScript variable to Flask url_for
Any ideas will be highly appreciated.