{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### Default Privacy1\n", "By default, `plotly.iplot()` and `plotly.plot()` create public graphs (which are free to create). With a [plotly subscription](https://plotly.com/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": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import chart_studio.plotly as py\n", "import plotly.graph_objects 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": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://plotly.com/~PythonPlotBot/2677/'" ] }, "execution_count": 2, "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": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "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": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://plotly.com/~PythonPlotBot/2679/'" ] }, "execution_count": 4, "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": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "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": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://plotly.com/~PythonPlotBot/475?share_key=UaGz0FTFLklnEd7XTKaqy8'" ] }, "execution_count": 6, "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": 7, "metadata": {}, "outputs": [], "source": [ "import chart_studio\n", "chart_studio.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": 8, "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://plotly.com/settings/api)" ] }, { "cell_type": "code", "execution_count": 9, "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": null, "metadata": {}, "outputs": [], "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://plotly.com/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": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function plot in module chart_studio.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", " 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": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "notebook_metadata_filter": "all", "text_representation": { "extension": ".md", "format_name": "markdown", "format_version": "1.1", "jupytext_version": "1.1.7" } }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" }, "plotly": { "description": "How to set the privacy settings of plotly graphs in python. Three examples of different privacy options: public, private and secret.", "display_as": "chart_studio", "has_thumbnail": true, "ipynb": "~notebook_demo/97", "language": "python", "layout": "base", "name": "Privacy", "order": 2, "permalink": "python/privacy/", "thumbnail": "thumbnail/privacy.jpg", "title": "Privacy | plotly", "v4upgrade": true } }, "nbformat": 4, "nbformat_minor": 2 }