==========================
Working with Code Snippets
==========================

Code Snippets enable you to securely upload reusable blocks of code to Anaconda Cloud. You can then share your snippets with other users, access your snippets in :doc:`Excel </excel/index>`, and insert your snippets across notebooks.

Anaconda maintains a Jupyter notebook that includes example code snippets you can modify and add to your own projects: |jupyter-notebook-badge|_

Creating Code Snippets
======================

Create a Code Snippet using the following steps:

#. Enter your code in an empty notebook cell.
#. Click |codesnippet| **Create a New Code Snippet** in the cell toolbar.
#. Enter a name for your code snippet.
#. Select a project to house your snippet.

    .. note::

        If you don't have any projects, a new project with the code snippet name as the title will be created.

#. Click **Create Code Snippet**.

A new ``.py`` file with the code snippet's name appears in the project and is ready to be used across your notebooks. Save the project by :ref:`pushing your project <sync-project>` to Anaconda Cloud to share your snippets across devices and software.

Sharing a snippet
=================

You can share a snippet with others in your organization by :ref:`sharing the project <share-project>` that contains the snippet. 

Inserting snippets in a notebook
================================

When you're ready to use a saved snippet, insert it in a notebook using the following steps:

#. Open Anaconda Toolbox.
#. Under **Code Snippets**, select **Manage Code Snippets**. Alternatively, under **Anaconda Cloud**, select **My Projects**.
#. You can now insert your snippet in one of two ways:

    .. tab-set::

        .. tab-item:: Insert in cell

            #. Select a cell in the notebook where you'd like to insert the code.
            #. Click |play| **Insert code snippet in notebook** beside the code snippet.

            The code snippet appears in a new cell below the selected cell. If no cell was selected, the code snippet appears in the last cell of the notebook.

        .. tab-item:: Copy code

            #. Click |action| actions beside the code snippet, then click **Copy to Clipboard**.
            #. Select a cell in the notebook where you'd like to insert the code.
            #. Paste the code into the cell. 

Editing a snippet
=================

To edit a snippet, you must make changes to the snippet directly in the Anaconda Toolbox extension. 

#. Open Anaconda Toolbox.
#. Under **Code Snippets**, select **Manage Code Snippets**. Alternatively, under **Anaconda Cloud**, select **My Projects**.
#. Double-click the snippet's ``.py`` file to open the file.
#. Adjust your snippet.
#. To save your file changes, go to **File**, then select *Save Python File*.

You can now use your updated snippet across your notebooks. However, you'll need to :ref:`push the associated project <sync-project>` to make the changes available in Anaconda Cloud.

.. |action| raw:: html

        <svg width="1em" height="1em" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
    <path d="M7.5 4C8.325 4 9 3.325 9 2.5C9 1.675 8.325 1 7.5 1C6.675 1 6 1.675 6 2.5C6 3.325 6.675 4 7.5 4Z"/>
    <path d="M7.5 6C6.675 6 6 6.675 6 7.5C6 8.325 6.675 9 7.5 9C8.325 9 9 8.325 9 7.5C9 6.675 8.325 6 7.5 6Z"/>
    <path d="M7.5 11C6.675 11 6 11.675 6 12.5C6 13.325 6.675 14 7.5 14C8.325 14 9 13.325 9 12.5C9 11.675 8.325 11 7.5 11Z"/>
    </svg>

.. |codesnippet| raw:: html

    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" data-icon="codesnippet:save"><path xmlns="http://www.w3.org/2000/svg" d="M13.7143 1.14286C14.3464 1.14286 14.8571 1.65357 14.8571 2.28571V13.7143C14.8571 14.3464 14.3464 14.8571 13.7143 14.8571H2.28571C1.65357 14.8571 1.14286 14.3464 1.14286 13.7143V2.28571C1.14286 1.65357 1.65357 1.14286 2.28571 1.14286H13.7143ZM2.28571 0C1.025 0 0 1.025 0 2.28571V13.7143C0 14.975 1.025 16 2.28571 16H13.7143C14.975 16 16 14.975 16 13.7143V2.28571C16 1.025 14.975 0 13.7143 0H2.28571ZM9.33571 5C9.1 5.21071 9.07857 5.57143 9.28929 5.80714L11.2357 8L9.28572 10.1929C9.075 10.4286 9.09643 10.7893 9.33214 11C9.56786 11.2107 9.92857 11.1893 10.1393 10.9536L12.425 8.38214C12.6179 8.16429 12.6179 7.83929 12.425 7.62143L10.1393 5.05C9.92857 4.81429 9.56786 4.79286 9.33214 5.00357L9.33571 5ZM6.71429 5.80714C6.925 5.57143 6.90357 5.21071 6.66786 5C6.43214 4.78929 6.07143 4.81071 5.86071 5.04643L3.575 7.61786C3.38214 7.83571 3.38214 8.16072 3.575 8.37857L5.86071 10.95C6.07143 11.1857 6.43214 11.2071 6.66786 10.9964C6.90357 10.7857 6.925 10.425 6.71429 10.1893L4.76429 8L6.71429 5.80714Z" fill="#31a824"></path></svg>

.. |play| raw:: html

    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 384 512"><path d="M48 432L336 256 48 80l0 352zM24.5 38.1C39.7 29.6 58.2 30 73 39L361 215c14.3 8.7 23 24.2 23 41s-8.7 32.2-23 41L73 473c-14.8 9.1-33.4 9.4-48.5 .9S0 449.4 0 432V80C0 62.6 9.4 46.6 24.5 38.1z"/></svg>

.. |jupyter-notebook-badge| image:: ../../img/icon/jupyter_notebook_badge.svg

.. _jupyter-notebook-badge: https://anaconda.cloud/share/notebooks/df27d9ad-b174-4874-b5e2-698882754ea1/overview