{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).\n",
" You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).\n",
" We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!\n",
"#### Version Check\n",
"Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.1.0'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly \n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Default Privacy\n",
"By default, `plotly.iplot()` and `plotly.plot()` create public graphs (which are free to create). With a [plotly subscription](https://plot.ly/plans) you can easily make charts private or secret via the sharing argument."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Public Graphs"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Scatter(\n",
" x=[1, 2, 3],\n",
" y=[1, 3, 1]\n",
" )\n",
"]\n",
"\n",
"py.iplot(data, filename='privacy-public', sharing='public')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'https://plot.ly/~jordanpeterson/1083'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py.plot(data, filename='privacy-public', sharing='public')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Private Graphs"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py.iplot(data, filename='privacy-private', sharing='private')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'https://plot.ly/~jordanpeterson/1085'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py.plot(data, filename='privacy-private', sharing='private')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Secret Graphs "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py.iplot(data, filename='privacy-secret', sharing='secret')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'https://plot.ly/~jordanpeterson/1087?share_key=mId9Rao9B6Pyh7UrNdPotP'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"py.plot(data, filename='privacy-secret', sharing='secret')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Make All Future Plots Private\n",
"To make all future plots private, you can update your configuration file to create private plots by default:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import plotly \n",
"plotly.tools.set_config_file(world_readable=False, sharing='private')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Make All Existing Plots Private\n",
"This example uses [Plotly's REST API](https://api.plot.ly/v2/)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import requests\n",
"from requests.auth import HTTPBasicAuth"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define variables, including YOUR [USERNAME and API KEY](https://plot.ly/settings/api)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"username = 'private_plotly' # Replace with YOUR USERNAME\n",
"api_key = 'k0yy0ztssk' # Replace with YOUR API KEY\n",
"\n",
"auth = HTTPBasicAuth(username, api_key)\n",
"headers = {'Plotly-Client-Platform': 'python'}\n",
"\n",
"page_size = 500"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Collect filenames of ALL of your plots and update `world_readable` of each plot with a PATCH request"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ALL of your plots are now private - visit: https://plot.ly/organize/home to view your private plots!\n"
]
}
],
"source": [
"def get_pages(username, page_size):\n",
" url = 'https://api.plot.ly/v2/folders/all?user='+username+'&filetype=plot&page_size='+str(page_size)\n",
" response = requests.get(url, auth=auth, headers=headers)\n",
" if response.status_code != 200:\n",
" return\n",
" page = json.loads(response.content.decode('utf-8'))\n",
" yield page\n",
" while True:\n",
" resource = page['children']['next']\n",
" if not resource:\n",
" break\n",
" response = requests.get(resource, auth=auth, headers=headers)\n",
" if response.status_code != 200:\n",
" break\n",
" page = json.loads(response.content.decode('utf-8'))\n",
" yield page\n",
"\n",
"def make_all_plots_private(username, page_size=500):\n",
" for page in get_pages(username, page_size):\n",
" for x in range(0, len(page['children']['results'])):\n",
" fid = page['children']['results'][x]['fid']\n",
" requests.patch('https://api.plot.ly/v2/files/'+fid, {\"world_readable\": False}, auth=auth, headers=headers)\n",
" print('ALL of your plots are now private - visit: https://plot.ly/organize/home to view your private plots!')\n",
"\n",
"make_all_plots_private(username)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function plot in module plotly.plotly.plotly:\n",
"\n",
"plot(figure_or_data, validate=True, **plot_options)\n",
" Create a unique url for this plot in Plotly and optionally open url.\n",
" \n",
" plot_options keyword arguments:\n",
" filename (string) -- the name that will be associated with this figure\n",
" fileopt ('new' | 'overwrite' | 'extend' | 'append') -- 'new' creates a\n",
" 'new': create a new, unique url for this plot\n",
" 'overwrite': overwrite the file associated with `filename` with this\n",
" 'extend': add additional numbers (data) to existing traces\n",
" 'append': add additional traces to existing data lists\n",
" auto_open (default=True) -- Toggle browser options\n",
" True: open this plot in a new browser tab\n",
" False: do not open plot in the browser, but do return the unique url\n",
" sharing ('public' | 'private' | 'secret') -- Toggle who can view this\n",
" graph\n",
" - 'public': Anyone can view this graph. It will appear in your profile\n",
" and can appear in search engines. You do not need to be\n",
" logged in to Plotly to view this chart.\n",
" - 'private': Only you can view this plot. It will not appear in the\n",
" Plotly feed, your profile, or search engines. You must be\n",
" logged in to Plotly to view this graph. You can privately\n",
" share this graph with other Plotly users in your online\n",
" Plotly account and they will need to be logged in to\n",
" view this plot.\n",
" - 'secret': Anyone with this secret link can view this chart. It will\n",
" not appear in the Plotly feed, your profile, or search\n",
" engines. If it is embedded inside a webpage or an IPython\n",
" notebook, anybody who is viewing that page will be able to\n",
" view the graph. You do not need to be logged in to view\n",
" this plot.\n",
" world_readable (default=True) -- Deprecated: use \"sharing\".\n",
" Make this figure private/public\n",
"\n"
]
}
],
"source": [
"help(py.plot)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-oEmPsN\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-NzZ88C/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
"Successfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'privacy.ipynb', 'python/privacy/', 'Privacy',\n",
" 'How to set the privacy settings of plotly graphs in python. Three examples of different privacy options: public, private and secret.',\n",
" title = 'Privacy | plotly',\n",
" name = 'Privacy', language='python',\n",
" has_thumbnail= True, thumbnail= 'thumbnail/privacy.jpg',\n",
" display_as='chart_studio', order=2,\n",
" ipynb= '~notebook_demo/97')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}