Quadratic Docs
  • Getting started
  • Examples
  • Open Quadratic
  • Quadratic AI
    • Getting started
    • Generate code
    • Generate data
    • Import PDFs
    • Import images
    • Security
  • Connections
    • SQL - getting started
    • PostgreSQL
    • MySQL
    • MS SQL Server
    • Parametrize queries
    • SQL AI assistant
    • Security
    • API requests
    • Request a connection
  • Python
    • Getting started
    • Reference cells
    • Return data to the sheet
    • Packages
    • Make an API request
    • Clean data
    • Charts/visualizations
    • Manipulate data
  • Javascript
    • Getting started
    • Reference cells
    • Return data to the sheet
    • API Requests
    • Charts/visualizations
    • Packages
  • Formulas
    • Getting started
    • AI assistant
    • Reference cells
    • Functions and operators
    • Arrays
    • Criteria
    • Wildcards
  • Spreadsheet
    • Navigating
    • Files
    • Shortcuts
    • Insert/delete rows and columns
    • Data validation
    • Present & share
    • Date-time formatting
    • Browser compatibility
  • Teams
    • Manage your team
    • Private files
    • Collaboration
    • Embedded sheets
  • Import data
    • SQL connections
    • API requests
    • Drag and drop .CSV
    • Drag and drop .Parquet
    • Import Excel files
  • Self hosting
    • Getting started
    • Docker
    • AWS
    • Azure
    • Google Cloud Platform
    • Bring your own AI
    • Other hosting
  • Quadratic for Education
    • Overview
    • Enrolling in the education plan
    • Teachers
    • Students
    • Researchers
    • Education FAQ
  • Company
    • About
    • Quadratic is source available
    • Brand assets
  • GitHub
  • Blog
  • Twitter
  • Discord
Powered by GitBook
On this page
  • 1. Single Value
  • 2. List of values
  • 3. DataFrame
  • 4. Charts
  • 5. Function outputs

Was this helpful?

  1. Python

Return data to the sheet

Return the data from your Python code to the spreadsheet.

PreviousReference cellsNextPackages

Last updated 2 months ago

Was this helpful?

Quadratic is built to seamlessly integrate Python to the spreadsheet. This means being able to manipulate data in code and very simply output that data into the sheet.

By default, the last line of code is output to the spreadsheet. This should be one of the five basic types:

  1. : for displaying the single number result of a computation

  2. for displaying a list of values from a computation

  3. for displaying the workhorse data type of Quadratic

  4. for displaying Plotly charts in Quadratic

  5. return the results of functions to the sheet

All code outputs by default are given names that can be referenced, regardless of their return type.

You can expect to primarily use DataFrames as Quadratic is heavily built around Pandas DataFrames due to widespread Pandas adoption in almost all data science communities!

1. Single Value

Note the simplest possible example, where we set x = 5 and then return x to the spreadsheet by placing x in the last line of code.

# create variable 
x = 5 

# last line of code gets returned to the sheet, so x of value 5 gets returned
x

2. List of values

Lists can be returned directly to the sheet. They'll be returned as tables with default column headings. You can edit or remove those headers in the table menu.

# create a list that has the numbers 1 through 5 
my_list = [1, 2, 3, 4, 5]

# returns the list to the spreadsheet 
my_list

3. DataFrame

You can return your DataFrames directly to the sheet by putting the DataFrame's variable name as the last line of code. DataFrames are returned to the sheet as Tables. The DataFrame's column names will be returned to the sheet as table headers.

# import pandas 
import pandas as pd
 
# create some sample data 
data = [['tom', 30], ['nick', 19], ['julie', 42]]
 
# Create the DataFrame
df = pd.DataFrame(data, columns=['Name', 'Age'])
 
# return DataFrame to the sheet
df

Note that if your DataFrame has an index it will not be returned to the sheet. If you want to return the index to the sheet use the following code:

# use reset_index() method where df is the dataframe name
df.reset_index()

An example of when this is necessary is any time you use the describe() method in Pandas. This creates an index so you'll need to use reset_index() if you want to correctly display the index in the sheet when you return the DataFrame.

4. Charts

Build your chart and return it to the spreadsheet by using the fig variable name or .show()

# import plotly
import plotly.express as px

# replace this df with your data
df = px.data.gapminder().query("country=='Canada'")

# create your chart type, for more chart types: https://plotly.com/python/
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')

# display chart, alternatively can just put fig without the .show()
fig.show()

5. Function outputs

You cannot use the return keyword to return data to the sheet, as that keyword only works inside of Python functions. Here is an example of using a Python function to return data to the sheet.

def do_some_math(x): 
    return x+1

# returns the result of do_some_math(), which in this case is 6 
do_some_math(5)

Note: Quadratic returns can only support a single variable being returned. For example, if I want to return a list I must return a variable that is a single list. I can not try to return a list by attempting to return multiple variables, e.g. I can not try x, y as a return statement. One must combine x and y into a single variable and return that single variable.

Single value
List of values:
DataFrame:
Chart:
Function outputs: