Anaconda Code (beta)
Anaconda Code empowers you to write Python code and run it locally, directly within Excel. This gives you flexibility and control over the Python environment in your workbook, allowing you to add and remove packages as needed, all while keeping code and data securely within your workbook.
Initializing Anaconda Code
Anaconda Code is included in the Anaconda Toolbox installation.
Anaconda Code is powered by PyScript, our open-source platform for running Python in the browser. When you first launch Anaconda Code, set up and run your PyScript Python environment using the following steps:
- Click Enable PyScript.
- Once PyScript is enabled, sign in to Anaconda Cloud.
Understanding Anaconda Code
Let’s take a look at the different elements within Anaconda Code using the Dashboard tab for reference:
Dashboard
Create and run Python code and view script logs
Imports and Definitions
Customize the code that affects all code in your workbook
Environment
Manage the packages and Pyodide version for your coding environment
Settings
Modify the default settings for running code
Account
View profile, subscriptions, and app details
Code
View and edit the code throughout your workbook
Logs
More
Quickly confirm your code’s cell linking states
New Code
Create new code to run in your workbook
Running Python code
Create an Anaconda Code cell that can run Python code using the following steps:
-
From the Dashboard, click New, then select a cell where you want to insert your code. Alternatively, select a cell, type =ANACONDA.CODE(, and press Enter (Windows)/Return (Mac).
-
Enter your Python code in the code editor. If you want to reference a range of data from your spreadsheet or an Anaconda Code object in your code, click Link Range and select the range of cells or the PyScript Data cell.
-
Set the cell linking and output options.
Toggle between isolated and linked modes by clicking the plug button.
-
Click Save and Run.
The selected cell is now an Anaconda Code cell. The Anaconda Code cell runs your code and displays the return value of the last expression.
If you write code that doesn’t have a return value (e.g., you define a function but don’t call the function) and click Run, the cell will display “None”.
Editing Python code
Do not edit your code in the cell itself; instead, modify and re-run your code directly in Anaconda Code.
An Anaconda.cloud account is required for users to edit shared code.
- From the Dashboard, click Edit in full view to open the edit view.
- Adjust your code, then click Run.
Managing the environment
Anaconda Code hosts a single, self-contained environment, which manages the back-end software packages that enable you to run Python code within your Excel workbook. You can manage software packages within this environment to extend Python’s processing, visualization, and analytical capabilities, and even select the version of Pyodide (the WASM engine used by PyScript) that you want to run Python.
You can make changes to your environment at any time; however, like with all software projects, altering the environment changes the way the underlying code is interpreted and can cause unintended complications.
Choosing a Pyodide version
The latest version of Pyodide is used by default for all new spreadsheets. For existing spreadsheets, the versions of Pyodide and packages necessary for your code are pinned to the environment.
You can switch versions of Pyodide using the following steps:
- From the Environment tab, click Edit.
- To switch versions of Pyodide, click the dropdown beside your current Pyodide version.
Managing software packages
- From the Environment tab, click Edit.
- To add new packages, click Add. Alternatively, click the arrow to add from either PyPI, the PyScript app, or a direct download link to a Python wheel (
.whl
).
Packages that contain compiled code might not be compatible with PyScript’s WASM engine. For more information, visit PyScript.net.
To remove a package, click Delete beside the package you want to remove.
Customizing code initialization
You can think of Anaconda Code’s Imports and Definitions as an initialization file for your code or like the first cell in a Jupyter Notebook. All code in this section is available to all cells, whether they are run isolated or linked.
Creating user-defined functions
User-defined functions (UDFs) allow you to write Python functions, decorate them, and call them directly from Excel.
Creating and calling a UDF
-
From the Imports and Definitions tab, decorate a function with
@UDF
, as shown in the following example: -
Click Save and Run.
-
In an open cell, enter
=ANACONDA
. If you added the example above to your definitions list, the option to callANACONDA.MY_CUSTOM_FUNCTION
appears in the dropdown. -
Arrow down to
ANACONDA.MY_CUSTOM_FUNCTION
, press Tab, and then complete the function. -
Use Ctrl+Enter (Windows)/Ctrl+Return (Mac) to run the code.
If you’d prefer the UDF uses a name other than the function name, use the name
argument to provide a unique name. Set nested
to False to remove ANACONDA.
from the name.
Using Range arguments
Specifying a UDF.Range
argument tells Excel that the input or output of the function is a 2D range. Without specifying this, Excel refuses to pass a range of cells as input or allow a spilled range to be returned from the function.
Example usage of .Range
:
You can also add type hints for Ranges using UDF.Range[str]
, for example.
Modifying workbook settings
While you can adjust the settings for running code in your workbook on a case-by-case basis when creating and editing code, you can also assign default settings from the Settings tab.
Cell linking
Mode | Description |
---|---|
Run Isolated | Code runs independently of other cells. Variables declared in previous Anaconda Code cells cannot be referenced. Other cells can reference the return value through the REF function. |
Run Linked | Anaconda Code cells run in row major order, comparable to how Python in Excel is executed. Variables declared in one cell can be referenced in cells following the cell that initializes them. When one cell is calculated, all linked cells recalculate in order. Linked cells run left-to-right, top-to-bottom, and can access objects defined in previously linked cells. |
Cell output
Output | Description |
---|---|
Excel Values | When outputting a DataFrame, array, list, etc., the values will “spill” to fill the required space. If the spill were to overwrite cells containing data, the cell displays a #SPILL error. |
Local Python Object | For certain object types, you can view the contents in a “Card View” by clicking the cell. You can reference this cell and the returned object like you would any other Python object. |
Troubleshooting
If you encounter an issue that is not listed here, you can obtain support for Anaconda through the Anaconda community or by opening a support ticket.
Error installing functions
Was this page helpful?