Forum Discussion

juan_pablo's avatar
juan_pablo
Icon for Helper V rankHelper V
6 years ago

Python visual requieres to open a file in the service

Hi everyone!

I have a python visual in my Power BI Desktop report, the script for this visual opens a python .pkl file to run a small model and with the results it generate the final plot (a simple plt.scatter) to be displayed in the report. Everything works fine in the PBI Desktop version on my local machine (the script opens the pkl file, runs the model and generates the plot in the PBI report), but when I upload the report to the server, for obvious reasons, the python script can't find the .pkl file required.

How can I make available the pkl file in the server so that the visual script can open it, generate the final plot and therefore the visual in the report?

 

I tried with a personal gateway but it seems it only works to access the .csv files for the data model in my local machine, but not the files required by a python script on a visual, or I don't know how to configure it properly.

 

Can you please help me in order to solve this?

Thanks in advance.!

9 Replies

    • juan_pablo's avatar
      juan_pablo
      Icon for Helper V rankHelper V

      lbendlin  like which one?
      I tried to place the whole code in the visual but that causes that the model gets trained every time a slicer or filter is selected (a query is made), and I already have the model pre trained.

      Do you have another way to do it without the pickle file? or any place I can read about similar solutions?

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        Try masquerading the .pkl as a binary resource, or place it on a web server that is accessible from Power BI Service.

  • v-yingjl's avatar
    v-yingjl
    Icon for Community Support rankCommunity Support

    Hi juan_pablo ,

    If you've fixed the issue on your own please kindly share your solution. If the above posts help, please kindly mark it as a solution to help others find it more quickly. Thanks!


    Best Regards,
    Yingjie Li

    • juan_pablo's avatar
      juan_pablo
      Icon for Helper V rankHelper V

      Hi v-yingjl 

       

      No, unfortunately I haven't found a solution. 

      I decided to open the file from an S3 in AWS (Amazon) as lbendlin  suggested. This work great in the Desktop version, but as soon as I publish the report to the Service, it breaks.

      Your solution didn't work for me because I still need to have interactivity in the visual (I need other filters to affect the visual because they are the input to the model).

       

      lbendlin Do you know how to make the connection to the web server accessible from Power BI Service?

       

      This is a summary of what I have so far:

       

      link = "https://xxxxxx.s3.amazonaws.com/models/linear_model_v1.pkl"
      def predict_sq_meter_value(apartments): 
          try: 
              lin_model_v0
          except: 
              lin_model_v0 = pickle.load(urllib.request.urlopen(link))
      
      ...

       

      The last line "pickle.load(urllib.request.urlopen(link))" is the one that breaks when published to the PBI Service, but in the Desktop  works as expected.

       

      This is the error I get in the published report

       

       

       

      Script Runtime Error
          self._send_request(method, url, body, headers, encode_chunked)
        File "C:\Python\Lib\http\client.py", line 1298, in _send_request
          self.endheaders(body, encode_chunked=encode_chunked)
        File "C:\Python\Lib\http\client.py", line 1247, in endheaders
          self._send_output(message_body, encode_chunked=encode_chunked)
        File "C:\Python\Lib\http\client.py", line 1026, in _send_output
          self.send(msg)
        File "C:\Python\Lib\http\client.py", line 966, in send
          self.connect()
        File "C:\Python\Lib\http\client.py", line 1414, in connect
          super().connect()
        File "C:\Python\Lib\http\client.py", line 938, in connect
          (self.host,self.port), self.timeout, self.source_address)
        File "C:\Python\Lib\socket.py", line 707, in create_connection
          for res in getaddrinfo(host, port, 0, SOCK_STREAM):
        File "C:\Python\Lib\socket.py", line 752, in getaddrinfo
          for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
      socket.gaierror: [Errno 11001] getaddrinfo failed
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "C:\Script\0.py", line 92, in <module>
          sq_val, tot_val = predict_sq_meter_value(property)
        File "C:\Script\0.py", line 39, in predict_sq_meter_value
          lin_model_v0 = pickle.load(urllib.request.urlopen(link))
        File "C:\Python\Lib\urllib\request.py", line 222, in urlopen
          return opener.open(url, data, timeout)
        File "C:\Python\Lib\urllib\request.py", line 525, in open
          response = self._open(req, data)
        File "C:\Python\Lib\urllib\request.py", line 543, in _open
          '_open', req)
        File "C:\Python\Lib\urllib\request.py", line 503, in _call_chain
          result = func(*args)
        File "C:\Python\Lib\urllib\request.py", line 1362, in https_open
          context=self._context, check_hostname=self._check_hostname)
        File "C:\Python\Lib\urllib\request.py", line 1321, in do_open
      
          raise URLError(err)
      
      urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>
      
      
      Please try again later or contact support. If you contact support, please provide these details.
      Activity ID: 28e08314-3838-462e-b4ec-5de753c8fb15
      Request ID: 80f03bdf-612e-aa34-1a41-e45de7239e1a
      Correlation ID: f0bea7a5-a996-d6a4-6786-0c24eecfbb45
      Time: Tue Jul 28 2020 10:39:59 GMT-0500 (Colombia Standard Time)
      Service version: 13.0.13960.55
      Client version: 2007.3.02011-train
      Cluster URI: https://wabi-south-central-us-redirect.analysis.windows.net/

       

       

      Any ideas on how can I solve this? I think I'm very close.

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        The service doesn't know how to reach the file on AWS. maybe an authentication issue?

         

        For fun you could try this on the gateway to see if you get a different error message there.  (yes, I know, gateway isn't really required)