{ "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!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Imports and Credentials\n", "In additional to importing python's `requests` and `json` packages, this tutorial also uses [Plotly's REST API](https://api.plot.ly/v2/)\n", "\n", "First define YOUR [username and api key](https://plot.ly/settings/api) and create `auth` and `headers` to use with `requests`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly\n", "import plotly.plotly as py\n", "\n", "import json\n", "import requests\n", "from requests.auth import HTTPBasicAuth\n", "\n", "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", "plotly.tools.set_credentials_file(username=username, api_key=api_key)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [Trash](https://api.plot.ly/v2/files/#trash) and [Restore](https://api.plot.ly/v2/files/#restore)\n", "Create a plot and return the url to see the file id which will be used to delete the plot. " ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "u'https://plot.ly/~private_plotly/52'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "url = py.plot({\"data\": [{\"x\": [1, 2, 3],\n", " \"y\": [4, 2, 4]}],\n", " \"layout\": {\"title\": \"Let's Trash This Plot
(then restore it)\"}},\n", " filename='trash example') \n", "\n", "url" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Include the file id in your request.
The file id is your `username:plot_id#`" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'private_plotly:18'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fid = username+':18'\n", "fid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following request moves the plot from the [organize folder](https://plot.ly/organize/home) into the trash.
Note: a successful trash request will return a `Response [200]`." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "requests.post('https://api.plot.ly/v2/files/'+fid+'/trash', auth=auth, headers=headers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now if you visit the url, the plot won't be there.
However, at this point, there is the option to restore the plot (i.e. move it out of trash and back to the organize folder) with the following request:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [PERMANENT Delete](https://api.plot.ly/v2/files/#permanent_delete)\n", "\n", "This request CANNOT!!!!!!! be restored. \n", "Only use `permanent_delete` when absolutely sure the plot is no longer needed.
" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "u'https://plot.ly/~private_plotly/79'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "url = py.plot({\"data\": [{\"x\": [1, 2, 3],\n", " \"y\": [3, 2, 1]}],\n", " \"layout\": {\"title\": \"Let's Delete This Plot
FOREVER!!!!\"}},\n", " filename='PERMANENT delete ex') \n", "url" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'private_plotly:79'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fid_permanent_delete = username+':79'\n", "fid_permanent_delete" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To PERMANENTLY delete a plot, first move the plot to the trash (as seen above):" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "requests.post('https://api.plot.ly/v2/files/'+fid_permanent_delete+'/trash', auth=auth, headers=headers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then [permanent delete](https://api.plot.ly/v2/files/#permanent_delete).
\n", "Note: a successful permanent delete request will return a `Response [204]` (No Content)." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "requests.delete('https://api.plot.ly/v2/files/'+fid_permanent_delete+'/permanent_delete', auth=auth, headers=headers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Delete All Plots and Grids PERMANENTLY!\n", "In order to delete all plots and grids permanently, you need to delete all of your plots first, then delete all the associated grids." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_pages(username, page_size):\n", " url = 'https://api.plot.ly/v2/folders/all?user='+username+'&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)\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)\n", " yield page\n", " \n", "def permanently_delete_files(username, page_size=500, filetype_to_delete='plot'):\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", " res = requests.get('https://api.plot.ly/v2/files/' + fid, auth=auth, headers=headers)\n", " res.raise_for_status()\n", " if res.status_code == 200:\n", " json_res = json.loads(res.content)\n", " if json_res['filetype'] == filetype_to_delete:\n", " # move to trash\n", " requests.post('https://api.plot.ly/v2/files/'+fid+'/trash', auth=auth, headers=headers)\n", " # permanently delete\n", " requests.delete('https://api.plot.ly/v2/files/'+fid+'/permanent_delete', auth=auth, headers=headers)\n", "\n", "permanently_delete_files(username, filetype_to_delete='plot')\n", "permanently_delete_files(username, filetype_to_delete='grid')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", " \"You should import from nbconvert instead.\", ShimWarning)\n", "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", " warnings.warn('Did you \"Save\" this notebook before running this command? '\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", " 'delete.ipynb', 'python/delete-plots/', 'Deleting Plots with the Python API',\n", " 'How to delete plotly graphs in python.',\n", " name = 'Deleting Plots', language='python', \n", " has_thumbnail='true', thumbnail= 'thumbnail/delete.jpg',\n", " display_as='chart_studio', order=9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.10" } }, "nbformat": 4, "nbformat_minor": 1 }