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.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group