02 Using Wolfram Notebooks as a Programming Environment
0 (0 Likes / 0 Dislikes)
This section of Introduction
to Wolfram Notebooks
is about notebook features
that are helpful for creating,
documenting and sharing
computer programs.
To illustrate some
of those features,
here is a notebook from a project
in image processing.
This notebook contains
programs and comments
and programming notes
all organized into sections,
and in the last section are inputs
that create interactive applications
and deploy those applications
in the Wolfram Cloud.
This input, for example,
gives an interactive application,
with sliders
for adjusting parameters
in an image processing program,
and the next input deploys
this application in the cloud.
Evaluating that input
gives a hyperlink,
and clicking the hyperlink,
brings up a web browser
that shows another
Wolfram notebook,
this time displayed
in the web browser,
and within this notebook
is the same interactive application.
Returning now to the notebook
on the desktop,
one of the most useful features
of notebooks for programming
comes from the overall structure
of a notebook itself.
Notebooks are organized into cells,
which are identified by cell brackets
on the right side of the window.
The different types of cells
provide natural places for
entering different types of content
like programs
and background information,
and anything else that might be
relevant to the project.
For larger projects,
it is convenient
to organize the project by
organizing the cells into groups,
like the section groups
in this notebook.
Section groups can be closed
by double-clicking
the grouping bracket,
to give an outline overview
of the project.
And to work on some part
of the program,
closed sections can be reopened
by double-clicking the same bracket
or by clicking this icon
in the heading of a closed cell.
At a more detailed level,
notebooks support
a number of features
to help with entering programs.
Starting with an empty notebook,
input is treated by default
as Wolfram Language code
in an input cell.
As these inputs
are being entered,
various buttons and menus
appear automatically
to help with entering
the program,
along with highlighting
and changing colors
and various other things
to help with spelling and syntax.
One of the buttons is this icon
below the left end
of the Cell Insertion Bar.
The Cell Insertion Bar
is this horizontal line
that indicates where
a new cell will appear.
Clicking that icon
brings up a menu of choices
for how the next input
should be handled.
The first choice is
Wolfram Language Input,
which is the default,
and the second choice
is Free-form Input,
which provides for entering
programs in ordinary language.
For example, choosing
Free-form Input and entering,
“remove small components
from an image”,
brings up a template for the
DeleteSmallComponents function.
Natural language input
can also be indicated
by entering an equal sign as
the first character in the input.
This input runs an image
processing operation called
DeleteBorderComponents.
For general programming,
free-form inputs like these
have the disadvantage
of often being ambiguous since
different phrasing or terminology
can give
different interpretations,
but there are also
many situations
where free-form input
is quite useful.
There are many programming tools
that pop up during input,
for example,
returning to the Import command,
as a file name is being entered,
a button labeled File Browser
appears that you can click
to bring up the operating system
file browser,
which can be used
to select a file.
That file browser
can also be accessed
by choosing the File Path item
under the Insert menu.
Perhaps the most conspicuous
programming tool that pops up,
is the Suggestions Bar,
which appears after each output
and gives a row of buttons
for operations that are
commonly done with that output.
The last output here is an image
so, the Suggestions Bar,
in this example,
gives buttons for operations
that are typically done on images.
Some operations are simple
and can be done right away,
like rotating the image.
For example, clicking
the rotate-90-degree-right button
enters and evaluates the input
for a rotating the image.
For more complicated operations,
like edge detection,
the buttons in the Suggestions Bar
do more complicated things.
For example, clicking
the detect features button
brings up a menu of choices,
one of which is edge detection,
which is an operation with
two parameters and an option.
Choosing that menu item
brings up this dialog box
for a setting up the operation.
After making some selections,
clicking Done
gives the Wolfram Language code
to produce the chosen result,
with the function arguments
and the options already filled in.
The Suggestions Bar does take up
a certain amount of space
on the computer screen,
of course.
So for the times
when you are not using it,
you can turn
the Suggestions Bar off
by clicking this little circle,
on the right side of the window.
After that you will see
a different circle,
which you can click to turn
the Suggestions Bar back on.
For now the Suggestions Bar
will be left off.
Another way of generating code,
or at least templates for code,
is through the menus that pop up
as commands are being entered.
For example, here is a short
Manipulate program
and as this program
is being entered,
there are several menus and
buttons that pop up automatically.
This particular program
gives a result
with a slider for adjusting
a parameter
in an image processing operation.
To take a closer look
at the menus that popped up
as that program
was being entered,
consider entering
that input again.
The first menu that pops up
is a menu of choices
for completing the name
of the command.
Choosing one of the items
in that menu completes the name.
And after that there is
another button that can be clicked
to bring up a menu of templates
for filling in the arguments
of that function.
This example shows choosing
the second template
and then filling in the template.
Similar menus and buttons
appear for each new command.
And as each placeholder
in the template is filled in,
you can use the TAB key
to move to the next placeholder.
The appearance option
is not part of the template,
so that can just be typed in.
At any point to get a reminder
of what the function arguments
are supposed to look like,
you can hover the mouse pointer
over the name of the function
and click the button
with the arrows on it,
which brings up this summary.
The other button,
the circle with the “i” in it,
brings up the documentation
for the function.
Template menus can also be shown
by choosing Make Template
from the Edit menu.
For example,
to access the template menu
for the Erosion function,
you can select
the Erosion command,
position the insertion point
immediately to the right
of that command
and choose Make Template
or use the keyboard shortcut
to bring up the template menu.
Here select the second template
and use that to complete the input.
At any point
while working on the program
the notebook can be saved
in the cloud.
Save to Cloud under the File menu,
saves the notebook
in the current cloud account
and Publish to Cloud
saves the notebook with a link
for sharing the notebook
with other people.
For example,
choosing Save to Cloud
saves the notebook in the cloud,
and after that you could move
to a web browser on this computer
or on another computer and open
the notebook in the cloud.
This shows the notebook opened
in a Wolfram|One account,
where the saved notebook
can be opened
to continue working on
the program in the web browser.
Editing, command completion,
template, menus and other features
continue to work here,
much as they did on the desktop.
Instead of a standalone application,
like the one in that example,
there are many
programming projects
where the result
is a function definition
or some other code
that is intended to be used
in other programs
or as part of a larger project.
For example,
here is a version of that example
where the concluding section
gives the definition of a function.
This particular function
can be applied to an image
to count droplets in the image.
One way of saving that function
so that it can be used
in another session
or in another program
is to first mark the cell
with the definition
as an initialization cell,
which can be done
by selecting the cell
and choosing Initialization Cell,
from the Cell Properties submenu
of the Cell menu.
This also adds a grey background
to the cell,
since the stylesheet
for this notebook
adds a gray background
to initialization cells.
Initialization cells are cells
that are marked to be evaluated
when a notebook is opened,
but a second use
of initialization cells
is in saving the notebook
in a form called a package
that can be used
to load and evaluate
the initialization cells
into another session.
For example, choosing Save As
from the File menu
brings up a dialog box
where you can choose
to save the notebook
as a Wolfram Language Package.
After that, you can load
the saved file
into another session
using the Get function,
which loads and evaluates
initialization cells,
to add the definition
of the DropletCounts function
to that session.
A convenient way to organize
the process of creating a package
is to start by choosing
Wolfram Language Package
from the New, Package Script
submenu under the File menu,
which gives a notebook
with styles and options
set up specifically to help
with creating packages.
Unlike other notebooks,
typing into this notebook gives
an initialization cell by default,
and saving this notebook
saves the notebook by default
as a Wolfram Language Package
rather than as a notebook.
This notebook editor
also has other features
for package development,
like these buttons for navigating
to particular function definitions
or to different sections
of the notebook.
That's the end of the examples
for this section.
You can find more information
on features for entering code
by opening
the Wolfram documentation
and searching for
predictive interface,
where there are descriptions
of the Suggestions Bar
and the command
completion menus
and other topics
that came up in this section.
The documentation also includes
a selection of workflows
that gives
step-by-step instructions
for accomplishing
many common tasks,
like this guide on various ways
of using free-form Input.
All of these features
have been designed
to be sufficiently intuitive;
in many cases if a button
or a menu or something pops up,
just going ahead
and clicking on it
is a good way to figure out
what it does and how to use it.