Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Plotly in Python from communicating with the network in any form

Is it possible to get Plotly (used from within Python) to be "strictly local"? In other words, is it possible to use it in a way that guarantees it won't contact the network for any reason?

This includes things like the program trying to contact the Plotly service (since that was the business model), and also things like ensuring clicking anywhere in the generated html won't either have a link to Plotly or anywhere else.

Of course, I'd like to be able to do this on a production machine connected to the network, so pulling out the network connection is not an option.

like image 392
PonyEars Avatar asked Jan 19 '16 09:01

PonyEars


1 Answers

Even a simple import plotly already attempts to connect to the network as this example shows:

import logging
logging.basicConfig(level=logging.INFO)
import plotly

The output is:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): api.plot.ly

The connection is made when the get_graph_reference() function is called while the graph_reference module is initialized.

One way to avoid connecting to plot.ly servers is to set an invalid plotly_api_domain in ~/.plotly/.config. For me, this is not an option as the software is run on the client’s machine and I do not want to modify their configuration file. Additionally, it is also not yet possible to change the configuration directory through an environment variable.

One work-around is to monkey-patch requests.get before importing plotly:

import requests
import inspect
original_get = requests.get

def plotly_no_get(*args, **kwargs):
    one_frame_up = inspect.stack()[1]
    if one_frame_up[3] == 'get_graph_reference':
        raise requests.exceptions.RequestException
    return original_get(*args, **kwargs)

requests.get = plotly_no_get
import plotly

This is surely not a full solution but, if nothing else, this shows that plot.ly is currently not meant to be run completely offline.

like image 151
Marcel M Avatar answered Sep 29 '22 00:09

Marcel M