0

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.

xavlock
  • 31
  • 2
  • 11

1 Answers1

0

You are mixing backend and frontend.

In this snippet

$.getJSON({{ url_for('scrape.start_scrape', domain=domain_v)|tojson}}, {})

All inside these curly braces {{}} are python side code. And the variable domain_v is frontend Javascript variable which you cannot pass backend function.

You have 2 options to fix this. Either you defined domain_v in the backend with URL parsing functionality too. Or you can remove url_for('scrape.start_scrape', domain=domain_v) and do the same in Javascript side.

n1md7
  • 2,854
  • 1
  • 12
  • 27
  • is there a way to get URL in the backend code? @harry – xavlock Aug 07 '20 at 19:53
  • how do we get the current URL in backend ? – xavlock Aug 21 '20 at 16:25
  • or is there a way to pass the variable from frontend to backend ? - @harry – xavlock Aug 21 '20 at 17:08
  • Here is how to get the current URL from the backend https://stackoverflow.com/questions/43062047/how-can-i-get-current-base-uri-in-flask . and for another question, you cannot pass a variable from one language to another but you can pass the data. So make AJAX request and pass whatever you want and backend can get it. For that, you need a specific endpoint that will expect that particular data from the client and then act accordingly. – n1md7 Aug 22 '20 at 10:51