Skip to content

bigfunctions > run_python

run_python

Signature

run_python(python_code, requirements, kwargs)

Description

Run any python_code.

For security reasons (sandboxing):

  • this function is rather slow (a new python environement is created for each query). You may prefer to create a dedicated python function for your use case. You can suggest a new bigfunction here if you want someone to create your function.
  • your python code won't have access to internet
  • not all python packages can be installed
Param Possible values
python_code Arbitrary python code (indented with 4 spaces).
requirements requirements as you would pass them to pip install (separated with space). Keep note that for security reasons, not all python packages can be installed
kwargs A json dict of variables. These variables will be defined and usable in your python code.
How sandboxing is done

The provided python_code will run in pyodide: a python distribution which runs in a chrome headless browser.

This simplifies the implementation of:

  • isolation between function calls,
  • installation of python packages,
  • isolation from the internet.

For every function call:

  • we init a new browser context,
  • download pyodide,
  • install python packages
  • run the code.

Examples

1. Basic Example

select bigfunctions.eu.run_python(
  '''
  return sum(range(10))
  '''
  , 
  null
  , 
  null
  )
select bigfunctions.us.run_python(
  '''
  return sum(range(10))
  '''
  , 
  null
  , 
  null
  )
select bigfunctions.europe_west1.run_python(
  '''
  return sum(range(10))
  '''
  , 
  null
  , 
  null
  )
+--------+
| result |
+--------+
| 45     |
+--------+

2. Some packages such as pandas can be installed and used.

select bigfunctions.eu.run_python(
  '''
  import pandas as pd
  return pd.Series(range(10)).sum()
  '''
  , 
  'pandas'
  , 
  null
  )
select bigfunctions.us.run_python(
  '''
  import pandas as pd
  return pd.Series(range(10)).sum()
  '''
  , 
  'pandas'
  , 
  null
  )
select bigfunctions.europe_west1.run_python(
  '''
  import pandas as pd
  return pd.Series(range(10)).sum()
  '''
  , 
  'pandas'
  , 
  null
  )
+--------+
| result |
+--------+
| 45     |
+--------+

3. Replace word passed as a variable by its stem

select bigfunctions.eu.run_python(
  '''
  import snowballstemmer
  stemmer = snowballstemmer.stemmer('english')
  stems = stemmer.stemWords(text.split())
  return ' '.join(stems)
  '''
  , 
  'snowballstemmer'
  , 
  to_json(struct(
    'care cared and caring' as text
  ))
  )
select bigfunctions.us.run_python(
  '''
  import snowballstemmer
  stemmer = snowballstemmer.stemmer('english')
  stems = stemmer.stemWords(text.split())
  return ' '.join(stems)
  '''
  , 
  'snowballstemmer'
  , 
  to_json(struct(
    'care cared and caring' as text
  ))
  )
select bigfunctions.europe_west1.run_python(
  '''
  import snowballstemmer
  stemmer = snowballstemmer.stemmer('english')
  stems = stemmer.stemWords(text.split())
  return ' '.join(stems)
  '''
  , 
  'snowballstemmer'
  , 
  to_json(struct(
    'care cared and caring' as text
  ))
  )
+--------+
| result |
+--------+
| go     |
+--------+