Clean workspace, plot pane, and console on re-run in Julia

I am used to starting all my Matlab scripts with clear all; close all; clc to ensure I am not looking at any old data or plots. I found Julia’s clearconsole() to be equivalent to Matlab’s clc, but don’t have working solutions for the other two Matlab commands yet. I mostly work in the Juno IDE and run scripts with the Play (“Run All”) button.

I understand that the Revise.jl package is supposed to help refresh the workspace now that workspace() is deprecated, but it doesn’t work for this simple test case. If I define x once and then comment that line out, it will continue to print each time I run without error.

using Revise
clearconsole()
#x=1
println(x)

I know I can hit “Stop” then “Play” to reset the workspace. However, that still doesn’t close old plots, and the time to first plot problem makes this option undesirable.

I found the “Forget All Plots” button in Juno’s plot pane, but I would like to have that functionality as a line in my script instead. Currently, it takes me three clicks to run a script again after I edit it (four if I include “Stop”).

  1. “Forget All Plots”
  2. Somewhere in the editor to put focus back on my current file.
  3. “Run All”

I would ideally like to re-run in a fresh environment with one click or keystroke, but any tips on a better Juno workflow would be appreciated.

For a “clear all” equivalent, you can use a module. If your script looks like this:

module MyModule
x = 3
end #module

Then, each time you re-run the script, you will see the message “WARNING: replacing module MyModule”, and any functions/variables that you created in there will be lost.
At the REPL, you can access the variables as e.g. MyModule.x.

If you want to compare the results before/after some change to the code, then re-name the module in your script and run it again. Now you have access to results of both runs, and can compute things like MyModule.x - MyNewModule.x.

Another option is to write functions instead of scripts.

2 Likes

This is a cool option that I will definitely use for testing script changes, but wrapping code in a module or function makes the Juno Workspace pretty useless for debugging at a glance. On the other hand, I know I am supposed to avoid global variables in Julia, so maybe I will need to come up with another method for rapid debugging. What is typically done to glance at all the variables inside a function and check that they have the correct type and value?

I try to wrap all my work in functions so that I am mostly dealing with local variables rather than lingering globals (unless I am doing very quick prototyping in the REPL). This also has the added benefit of faster performance but ofcourse, when prototyping or quick data analysis, optimization/speed isn’t a big concern.

1 Like

The workspace works for all globals and globals in modules are still, well, global.

The workspace doesn’t seem to be showing my variables when I run this script.

Click on the little box in the top-right corner and select “MyModule”.
Screen Shot 2020-05-14 at 20.37.23

2 Likes

Ooo, got it! That makes modules much more useful. :grin:

I guess the last point to address in my question then is if there is a better method to manage plots?

(I will probably post a separate question about best methods to debug functions.)

We could add a function to Juno.jl that allows you to clear the plot pane programatically. Please open an issue at Sign in to GitHub · GitHub.

1 Like

I created a new Juno issue on Github about a function to clear the plot pane. Thank you for the suggestions!

1 Like

Does Visual Studio Code have clearconsole() or clearplotpane() equivalent commands now that the IDE of choice seems to be VS Code rather than Atom?

No. Feel free to open a feature request here.