Privacy in Python
How to set the privacy settings of plotly graphs in python. Three examples of different privacy options: public, private and secret.
New to Plotly?
Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
Default Privacy¶
By default, plotly.iplot()
and plotly.plot()
create public graphs (which are free to create). With a plotly subscription you can easily make charts private or secret via the sharing argument.
Public Graphs¶
import chart_studio.plotly as py
import plotly.graph_objects as go
data = [
go.Scatter(
x=[1, 2, 3],
y=[1, 3, 1]
)
]
py.iplot(data, filename='privacy-public', sharing='public')
Below is the URL of this public plot. Anyone can view public plots even if they are not logged into Plotly. Go ahead and try it out:
py.plot(data, filename='privacy-public', sharing='public')
Private Graphs¶
py.iplot(data, filename='privacy-private', sharing='private')
Below is the URL of the private plot above. Only the owner can view the private plot. You won't be able to view this plot, try it out:
py.plot(data, filename='privacy-private', sharing='private')
Secret Graphs¶
py.iplot(data, filename='privacy-secret', sharing='secret')
Below is the URL of this secret plot. Anyone with the secret link can view this chart. However, it will not appear in the Plotly feed, your profile, or search engines. Go ahead and try it out:
py.plot(data, filename='privacy-secret', sharing='secret')
Make All Future Plots Private¶
To make all future plots private, you can update your configuration file to create private plots by default:
import chart_studio
chart_studio.tools.set_config_file(world_readable=False, sharing='private')
Make All Existing Plots Private¶
This example uses Plotly's REST API
import json
import requests
from requests.auth import HTTPBasicAuth
Define variables, including YOUR USERNAME and API KEY
username = 'private_plotly' # Replace with YOUR USERNAME
api_key = 'k0yy0ztssk' # Replace with YOUR API KEY
auth = HTTPBasicAuth(username, api_key)
headers = {'Plotly-Client-Platform': 'python'}
page_size = 500
Collect filenames of ALL of your plots and
update world_readable
of each plot with a PATCH request
def get_pages(username, page_size):
url = 'https://api.plot.ly/v2/folders/all?user='+username+'&filetype=plot&page_size='+str(page_size)
response = requests.get(url, auth=auth, headers=headers)
if response.status_code != 200:
return
page = json.loads(response.content.decode('utf-8'))
yield page
while True:
resource = page['children']['next']
if not resource:
break
response = requests.get(resource, auth=auth, headers=headers)
if response.status_code != 200:
break
page = json.loads(response.content.decode('utf-8'))
yield page
def make_all_plots_private(username, page_size=500):
for page in get_pages(username, page_size):
for x in range(0, len(page['children']['results'])):
fid = page['children']['results'][x]['fid']
requests.patch('https://api.plot.ly/v2/files/'+fid, {"world_readable": False}, auth=auth, headers=headers)
print('ALL of your plots are now private - visit: https://plotly.com/organize/home to view your private plots!')
make_all_plots_private(username)
Reference¶
help(py.plot)