Anaconda Code #
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#
Note
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
View errors and print statements
- More
Quickly confirm your code’s cell linking states
- New code
Create new code to run in your workbook
Running Python code#
Begin writing Python code in a cell using the following steps:
From the Dashboard, click New, then select a cell where you want to insert your code.
Enter your Python code in the code editor. If you want to reference a range of data from your spreadsheet in your code, click Link Range and select the desired data range.
Using the
REF
functionThe
REF
function returns a list of lists and can be used in the following ways:Function
Use case
Notes
to_df(REF(<EXCEL_RANGE>))
Create a DataFrame
to_df
assumes your data has headersto_array(REF(<EXCEL_RANGE>))
Create a NumPy array
to_array
assumes all data is of the same typeYou can change the behavior of
to_df()
andto_array()
from the Imports and Definitions tab.Set the cell linking and output options.
Note
Toggle between isolated and linked modes by clicking the plug button.
Click Save and Run.
Your code runs in the designated cell.
Editing Python code#
Do not edit your code in the cell itself; instead, modify and re-run your code directly in Anaconda Code.
Note
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.
Note
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
).
Note
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.
To customize your code’s Imports and Definitions:
From the Imports and Definitions tab, establish the connections to the packages you need to run your code.
Note
You can only
import
from the packages included in the standard Python installation and those listed in the Environment tab.Click Run.
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:@UDF def my_custom_function(x, y): return x ** y
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/return to run the code.
Tip
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.
@UDF(name="MYBANK.PORTFOLIO_ANALYSIS", nested=False)
def my_custom_function(x, y):
return x ** y
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
:
@UDF
def square_me(data: UDF.Range) -> UDF.Range:
return [[val ** 2 for val in row] for row in data]
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 PyScript cells cannot be referenced. Other cells can reference the return value through the |
Run Linked |
PyScript 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 |
|
---|---|
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#
Cause
This error can occur when Excel loads the Anaconda Toolbox add-in and registers its custom functions. This error happens within Excel and cannot be resolved by the Anaconda Toolbox.
Solution
Close and reopen Excel. If the issue persists, uninstall the Anaconda Toolbox add-in, then reinstall.