0

The purpose of this form is to let users enter a lot of places (comma separated) and it'll retrieve the phone, name, website. Have it working in a python IDE, no problem, but having issues putting it into my webapp.

I'm getting the error Exception Value: Can't pickle local object 'GetNums.<locals>.get_data' at the line where a is assigned. I checked the type of inputText and verified that it is indeed a list. So, I'm not sure why it won't pickle.

def GetNums(request):
    form = GetNumsForm(request.POST or None)
    if form.is_valid():
      inputText = form.cleaned_data.get('getnums')

      # all experimental
      inputText = inputText.split(',')
      def get_data(i):
        #DON'T FORGET TO MOVE THE PRIMARY KEY LATER TO SETTINGS
          r1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + i + '&key=GET_YOUR_OWN')
          a = r1.json()
          pid = a['results'][0]['place_id']  
          r2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=' + pid + '&key=GET_YOUR_OWN')
          b = r2.json()
          phone = b['result']['formatted_phone_number']
          name = b['result']['name']
          try:
              website = b['result']['website']
          except:
              website ='No website found'
          return ' '.join((phone, name, website))


      v = str(type(inputText))
      with Pool(5) as p:
        a = (p.map(get_data, inputText))
      #   for line in p.map(get_data, inputText):
      #       print(line)
      #code assist by http://stackoverflow.com/a/34512870/5037442


      #end experimental

      return render(request, 'about.html', {'v': a})
click here
  • 814
  • 2
  • 10
  • 24

1 Answers1

2

It's actually barfing when trying to pickle get_data, which is a nested function/closure.

Move get_data out of GetNums (and agh rename it to snake_case please) and it should work.

roippi
  • 25,533
  • 4
  • 48
  • 73