Write and run SQL and Python code in a Snowflake Notebook

When you create a notebook, three example cells are displayed. You can modify those cells or add new ones.

Write SQL code in a notebook

To start writing SQL code in a notebook, add a SQL cell.

When writing SQL in a SQL cell, consider the following:

  • SQL cells must only contain a single statement per cell, and must be valid Snowflake SQL.

  • You cannot partially run a cell.

For other limitations, see Considerations for writing notebooks and adding cells.

Write Snowpark Python code in a notebook

To start writing Python code in a notebook, add a Python cell.

Snowflake Notebooks support the same Python version supported by Snowpark, Python 3.8.

When you create a notebook, the first Python cell imports the streamlit library and creates a Snowpark session. The Streamlit library is required to run a Snowflake Notebook and display visualizations.

A Snowflake Notebook creates a Snowpark session, so you can use most of the methods available in a Snowpark Session class. However, because a notebook runs inside Snowflake rather than in your local development environment, you cannot use the following methods:

  • session.add_import

  • session.add_packages

  • session.add_requirements

When you run a Python cell, output from the following functions is displayed in the results:

  • Functions that write to the console, such as print().

  • Functions that print a DataFrame, such as the show method of the DataFrame class in Snowpark Python.

You can import Python libraries to use in a notebook. See Import packages to use in notebooks for details.

Import packages to use in notebooks

Snowflake Notebooks include Streamlit and the third-party packages listed in the Snowflake Anaconda channel.

After your organization administrator accepts the terms, you can import libraries to use in Snowflake Notebooks:

  1. Sign in to Snowsight.

  2. Select Notebooks.

  3. Select a specific notebook for which you want to install Python packages.

  4. (Optional) Select Packages.

  5. Search for packages listed in the Snowflake Anaconda channel, such as numpy, pandas, requests, and urllib3.

  6. Select a package to install it for use in your notebook, and optionally change the default package version in the list of Installed Packages.

    Packages installed by you appear under Installed Packages.

  7. To use the previously installed libraries, add import statements to the code in a Python cell.

    For example, to use the previously installed scikit-learn package, add an import statement for that package at the beginning of your code:

    import scikit-learn
    
    Copy

참고

Packages that you add to a notebook are available only to that notebook. If you want to use the same package in a different Snowflake Notebook, you must use this procedure to add the package to that notebook.

Write Markdown in a notebook

To write Markdown in your notebook, add a Markdown cell:

  1. Use a keyboard shortcut and select Markdown, or select + Markdown.

  2. Select the Edit markdown pencil icon, and start writing Markdown.

You can type valid Markdown to format a text cell. As you type, the formatted text appears below the Markdown syntax.

Screenshot of a Markdown cell showing Markdown text with an H1 header indicated with a # and a header of An example Markdown cell followed by body text of This is an example Markdown cell in a Snowflake Notebook. Below the raw Markdown content, the rendered Markdown appears with a different font.

To view only the formatted text, select another cell. When the Markdown cell is no longer selected, only the rendered Markdown is visible.

Screenshot of a Markdown cell that is no longer selected and showing only the rendered Markdown: a header of An example Markdown cell and body text of This is an example Markdown cell in a Snowflake Notebook.

Visualize results of cells by using Streamlit

You can visualize the results of cells using one of the following libraries:

Visualize results by using Streamlit

You can use any chart elements supported by Streamlit version 1.22.0 to create a line chart, bar chart, area chart, scatter plot, or a map with points on it. See Chart elements in the API reference of the Streamlit library documentation.

참고

Some Streamlit chart elements are not supported in Snowflake or might be subject to additional terms. See Considerations for writing notebooks and adding cells.

For example, to reference a SQL cell CELL1 that outputs two columns of data as a table, where CELL1 includes the following SQL code:

SELECT 'FRIDAY' as SNOWDAY, 0.2 as CHANCE_OF_SNOW
UNION ALL
SELECT 'SATURDAY',0.5
UNION ALL
SELECT 'SUNDAY', 0.9;
Copy

To visualize the results in a line chart, you can use the following Python code to reference the results of the SQL cell, convert it to a Snowpark DataFrame and then a Pandas DataFrame, then display a line chart:

import streamlit as st

my_df = cells.CELL1.to_df().to_pandas()

st.line_chart(my_df, x='SNOWDAY', y='CHANCE_OF_SNOW')
Copy

After you run both cells, the following visualization appears:

Line chart with the x-axis labeled SNOWDAY and a y-axis of CHANCE_OF_SNOW with three points connected with a line on the graph, one for Friday showing a value of 0.2, one for Saturday showing a value of 0.5 and one for Sunday showing a value of 0.9 almost off the graph.

For details on referencing other cells, see Reference results and chain cells in a notebook.

Visualize results by using Altair

To visualize results using Altair visualizations, use the Streamlit st.altair_chart element.

Snowflake Notebooks currently support Altair version 4.0.

For example, to display a heat map based on the output of SQL cell CELL3, where CELL3 includes the following SQL code:

SELECT 1 as "a", 1 as "b", 35 as "c"
UNION ALL
SELECT 17, 7, 2
UNION ALL
SELECT 14, 29, 18;
Copy

Write the following Python code in a Python cell to reference the output of the SQL cell, process the output into a Snowpark DataFrame and then a Pandas DataFrame, create an Altair chart object, and then use Streamlit to display the chart:

import streamlit as st
import altair as alt

my_df = cells.CELL3.to_df().to_pandas()

chart = (
  alt.Chart(my_df)
  .mark_rect()
  .encode(x="a", y="b", color="c")
)

st.altair_chart(chart, use_container_width=False, theme=None)
Copy

After you run both cells, the following visualization appears:

Heat map with an x-axis labeled a and a y-axis labeled b, with a large teal rectangle covering most of the graph and a smaller yellow green rectangle next to it on the x-axis, much smaller. There is a legend labeled c that shows that the yellow green color indicates a value of less than 10 and the teal indicates a value around 20.

For details on referencing other cells, see Reference results and chain cells in a notebook.

Visualize results with Matplotlib

To visualize results using Matplotlib visualizations, use the Streamlit st.pyplot element.

To use a Matplotlib visualization, install the Matplotlib library for your notebook:

  1. From the notebook, select Packages.

  2. Locate the matplotlib library and select the library to install it.

  3. Write code to visualize your results.

For example, to visualize a bar chart of a random array of numbers, add the following code to a Python cell:

import streamlit as st
import matplotlib.pyplot as plt
import numpy as np

arr = np.random.normal(1, 1, size=100)
fig, ax = plt.subplots()
ax.hist(arr, bins=20)

st.pyplot(fig)
Copy

After you run the cell, the following visualization appears:

Bar chart with an x-axis showing values from 0 to 4 and a y-axis showing values from 0 to 13, with 20 dark blue bars of varying values.

For more details on using the st.pyplot chart element, see st.pyplot.

참고

Visualizations created with this library are not cached.

Visualize results with seaborn

To visualize results using seaborn visualizations, use the Streamlit st.pyplot element in combination with the seaborn library.

To use a seaborn visualization, you must install both the seaborn library and the Matplotlib library for your notebook:

  1. From the notebook, select Packages.

  2. Locate the seaborn library and select the library to install it.

  3. Locate the matplotlib library and select the library to install it.

  4. Write code to visualize your results.

To use seaborn, use the st.pyplot, but instead of passing a Matplotlib figure into the fig parameter, pass a seaborn figure.

For example, to display a bar chart based on the output of SQL cell CELL0, where CELL0 includes the following SQL code:

SELECT 1 as "a", 1 as "b", 35 as "c"
UNION ALL
SELECT 17, 7, 2
UNION ALL
SELECT 14, 29, 18;
Copy

Write the following Python code in a Python cell:

import streamlit as st
import seaborn as sns
import matplotlib.pyplot as plt

f, ax = plt.subplots(figsize=(3, 3))
fig=f.figure

my_df = cells.CELL0.to_df().to_pandas()

sns.set_color_codes("pastel")
sns.barplot(x="a", y="b", data=my_df,
            label="Total", color="c")

st.pyplot(fig)
Copy

After you run both cells, the following visualization appears:

Bar chart with an x-axis labeled a and a y-axis labeled b, with three pale green bars showing data values of 1, 29, and 7 from left to right.

For more details on using the st.pyplot chart element, see st.pyplot.

For examples of seaborn visualizations, see the seaborn Example gallery.

참고

Visualizations created with this library are not cached.

Reference results and chain cells in a notebook

You can reference the results of previous cells in a cell in your notebook.

For example, if you want to filter or visualize a table produced by a SQL or Python cell, refer to the following table:

Reference cell type

Current cell type

Reference syntax

Example

SQL

Python

cells.CELL1

The cell name of the reference is case-sensitive, and you must include the notation to convert the SQL table of results to a Snowpark DataFrame.

st.line_chart(cells.CELL1.to_df())
Copy

SQL

SQL

{{cells.CELL2}}

The cell name of the reference is case-sensitive and must exactly match the name of the referenced cell.

SELECT * FROM {{cells.CELL2}} where PRICE > 500
Copy

Python

SQL

{{variable}}

For example, in a Python cell named CELL1:

d = {'col1': [1, 2], 'col2': [3, 4]}
Copy

You can reference the value of the variable d in a SQL cell named CELL2 by referencing the variable:

SELECT * FROM my_table WHERE COUNTRY = {{d}}
Copy

Python

Python

cells.CELL1

Reference results from another Python cell as a variable. The cell name of the reference is case-sensitive and must exactly match the name of the referenced cell. It references the return value of the last statement in the referenced cell.

For example, in a Python cell named CELL1:

d = {'col1': [1, 2], 'col2': [3, 4]}
Copy

If the variable is called in the Python cell, you can reference the value of the variable d in a Python cell named CELL2 by referencing CELL1 from a different Python cell:

st.line_chart(cells.CELL1)
Copy

You can also reference the variable directly:

st.line_chart(d)
Copy

Run code and SQL in Snowflake Notebooks

To run code and SQL commands in a Snowflake Notebook, you can:

  • Run a single cell: Choose this option when making frequent code updates.

    • Press CMD + Return on a Mac keyboard, or CTRL + Enter on a Windows keyboard.

    • Select Run this cell only, or Run this cell only.

  • Run all cells in a notebook in sequential order: Choose this option before presenting or sharing a notebook to ensure that the recipients see the most current information.

    • Press CMD + Shift + Return on a Mac keyboard, or CTRL + Shift + Enter on a Windows keyboard.

    • Select Run all.

  • Run a cell and advance to the next cell: Choose this option to run a cell and move on to the next cell more quickly.

    • Press Shift + Return on a Mac keyboard, or Shift + Enter on a Windows keyboard.

    • Select the vertical ellipsis (more actions) for a cell, and choose Run cell and advance.

  • Run all above: Choose this option when running a cell that references the results of earlier cells.

    • Select the vertical ellipsis (more actions) for a cell, and choose Run all above.

  • Run all below: Choose this option when running a cell that later cells depend on. This option runs the current cell and all following cells.

    • Select the vertical ellipsis (more actions) for a cell, and choose Run all below.

Warehouse recommendations for running Snowflake Notebooks

When you create a notebook, you select a warehouse in which to run the Snowflake Notebook processes and any code. Snowflake recommends using an X-Small warehouse to minimize credit consumption.

You might want to run large queries using SQL or perform compute-intensive operations using Snowpark Python that require a larger warehouse. In this case, you have two options:

  • Specify a warehouse to use for a specific cell or set of cells.

    This ensures that the queries in those cells use the larger warehouse, but notebook processes continue to run on the smaller notebook warehouse, so you’ll use less compute when you’re not running queries.

    For example, to specify a warehouse called notebooks to use for specific cells, you can add a SQL cell with the USE WAREHOUSE command or add the command to the Python code in a Python cell:

    SQL::
    USE WAREHOUSE notebooks;
    
    Copy
    Python::
    session.sql('USE WAREHOUSE notebooks;').collect()
    
    Copy

    After you run any cell specifying USE WAREHOUSE, all cells that run in the remainder of the notebook session use the specified warehouse. If you no longer need queries to run with a large warehouse, write a new cell to specify a smaller warehouse to use.

  • Choose a larger warehouse for the entire notebook.

    If you choose this option, all queries that run in the notebook have more compute available, but the warehouse runs for the entire notebook session, even if queries do not run.

    조심

    Choosing this option can dramatically increase credit consumption for the entire notebook.

    To change the notebook warehouse:

    1. Select the notebook title, and then select Notebook settings.

    2. Select a new warehouse to use.

Keyboard shortcuts for notebooks

Snowflake Notebooks support various keyboard shortcuts to help accelerate your development process.

Task

MacOS

Windows

Open the list of keyboard shortcuts.

?

?

Run all cells.

CMD + Shift + Return

CTRL + Shift + Enter

Run the selected cell.

CMD + Return

CTRL + Enter

Run the selected cell and advance to the next cell.

Shift + Return

Shift + Enter

Move between cells.

Up and down arrows

Up and down arrows

Find within the cell.

CMD + f

CTRL + f

Add a cell above the currently selected cell.

a

a

Add a cell below the currently selected cell.

b

b

Delete the currently selected cell.

dd

dd

Convert a SQL or Python cell into a Markdown cell.

m

m

Convert a cell into a code cell:

  • Change a Markdown cell to a Python cell.

  • Change a Python cell to a SQL cell.

  • Change a SQL cell to a Python cell.

y

y

In addition, you can use the same keyboard shortcuts that you use for worksheets. See 바로 가기 키로 워크시트 업데이트하기.