cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Scala notebooks don't automatically print variables

tbao
New Contributor

It seems like with Scala notebook if I declare some variables or have important statements then the cell runs it will automatically print out the variables and import statements. Is there a way to disable this, so only explicit println are output?

1 REPLY 1

VZLA
Databricks Employee
Databricks Employee

There isn't a configuration that can be set to True/False and control this behavior for some statements.

This output is part of Databrick's interactive notebook design, where all evaluated statements—such as imports, variable declarations, and expressions—are displayed to help users keep track of variable states and imports in each cell.

As a workaround, you could manually control this indirectly with a { ... ; () } structure, as returning Unit suppresses implicit output. e.g.:

{
    // Imports
    import java.time.LocalDate
    import scala.util.Random
    
    // Variable declarations
    val currentDate = LocalDate.now()
    val randomValue = Random.nextInt(100)
    
    // Explicit outputs
    println(s"Today's date: $currentDate")
    println(s"Random value: $randomValue")

    // Suppress implicit output
    ()
}

Using the { ... ; () } structure does not impact code execution: imports, variable declarations, and other statements will still execute as usual but won't produce any implicit output; so this way, you may control what is displayed in the output of the Databricks notebook cell. Println produces explicit output.

Hope it helps.