In-class Notebook: Command line interface#

Day 2#

CMSE 202#

Put your name here

#

From https://blog.ssdnodes.com/blog/blinking-cursor-distant-server/

Learning goals for today’s class:#

By the end of class, you should:

  • Be more comfortable using the command line interface to:

    • create and move between directories on your computer

    • move, copy, and edit files

    • download files from the internet

    • run Python scripts

  • Be able to Enter the “vi” text editor, edit a file, and gracefully exit the text editor (all from starting on the command line)

  • Be able to run command line commands from inside of a Jupyter notebook

Agenda for today’s class#

  1. Revisiting common commandline commands

  2. Downloading files from the command line using curl

  3. Setting up a CMSE 202 directory using the command line

  4. Downloading and editing a simple (broken) Python script

  5. Modifying another Python script, but this time with the vi text editor

  6. Testing out command line commands from inside a Jupyter notebook


1. Revisting common command line commands#

✅ Do this: Before you dive into today’s activity, take a moment to remind yourself of some of the commands that came up in the pre-class assignment. If you don’t remember what they do, check in your with your group or your instructor.

Note: there might be a few in this list that didn’t come up, so make note of them and figure out what they do!

  • clear

  • pwd

  • ls

  • cd [directory]

  • cd ..

  • cd .

  • cd ~

  • cd -

  • cd /

  • touch [new file]

  • cp

    • cp [file] [new file]

    • cp -r [folder] [new folder]

  • mv

    • mv [path to file/folder] [new path to file/folder] (note that this can be used for relocating files or renaming files)

  • rm

    • rm [file]

    • rm -r [folder]

  • less [file]

  • more [file]

✅ Do this: Remind yourself how you can interact with Python on the commandline as well by revisiting these commands:

  • python

    • import random

    • random.randint(1,100)

    • exit()

  • ipython

    • import numpy as np

    • np.linspace(0,100,5)

    • ls

    • pwd

    • cd

    • exit()


2. Downloading files from the command line using curl#

Not only can you create, move, copy, and delete files on the command line, there are also commands that allow you to download files from the internet.

One such command for doing this is the curl command. Since you may not be familiar with this command, one option would be to do a quick google search for “curl unix command” which would bring up a page like this one: https://www.tutorialspoint.com/unix_commands/curl.htm

Of course, most command line commands also have a built-in set of documentation that explains what the command does and how it works. You can access this using the man command, to access the “manual” for that command.

✅ Do This: If you haven’t already, save this notebook and upload it to JupyterHub and continue working on it there. Then, open up a new Terminal in JupyterHub. Once you’re there, try running the following command:

man curl

(You’re also welcome to do this locally on your computer if you’ve set up a command line interface that works on your computer)

This command should bring up the manual for the curl command. You can step through this file one line at a time by pressing Return or Enter on your keyboard, or if you want to page through a page at a time you can press F or B to move forward and backward respectively. To quit out of the manual page, press Q.

✅ Do This: Now that you can access the manual for the curl command, use it or the internet link provided above to answer the following questions:

Do This - Edit this markdown to answer the questions below.

  1. What information does the curl command need to download a file from the internet?

Put your answer here.

  1. Does curl save the data to a file by default or does it just output to the terminal? If it doesn’t output it to a file by default, what flag (using - or --) do you need to use to make it do so? Is there more than one flag that can do this? To test this out, you can experiment a bit by using the README.md file from supplemental repository for the course:

    • https://raw.githubusercontent.com/msu-cmse-courses/cmse202-F22-data/main/README.md

Put your answer here.

✅ Do This: Using what you’ve learned, what would be the command to download the README.md file from the course website such that it is saved using the same filename?

Do This - Erase this and put your answer here.

✅ Do This: Confirm that you’re right by running your command on your terminal (if you haven’t done so already). Once you’re sure it works as intended, feel free to delete that file using rm.


3. Setting up a CMSE 202 directory using the command line#

If you’ve haven’t already, in this section you’re going practice setting up a directory structure on JupyterHub (or your computer if you’re working on things locally) for storing content related to CMSE 202.

✅ Do This - Using the command line interface, do the following:

  • Create a new directory in your home directory called CMSE202 (if you can’t remember which command to use, ask your group for help!)

  • Then, inside the CMSE202 directory, create two additional directories: notebooks and scripts (remind yourself how to change directories)

  • Double-check that everything worked by running the pwd and ls commands to poke around in your new directories.

  • If you already have some 202 notebooks on JupyterHub (or your computer), try using the mv command to move them into your new directories – this might be a little tricky at first!

🛑 STOP#

Check in with an instructor and show your work!


4. Downloading and editing a simple (broken) Python script#

Now that you have a directory structure set up for CMSE 202, let’s download a Python script into your new scripts directory.

✅ Do This: Make sure to navigate into your scripts directory on the command line and then use curl to download the following script (make sure you use the right flag so that it is saved as “fib.py”:

https://raw.githubusercontent.com/msu-cmse-courses/cmse202-F22-data/main/code_samples/fib.py

✅ Do This: Now that you have the file downloaded, do the following:

  1. Run the script on the command line using Python and look at the results.

  2. Open the file and take a look at the comments. Do you understand what the code is doing? Is there anything in the code that you’ve never seen before? If so, discuss these new components with your group and see if you can make sense of them. Specifically, do you understand what the following code is doing?

if __name__== "__main__":
    main()
  1. Is the code correctly doing what it is meant to do? If not, can you figure out what’s going wrong?

Is the cell below, explain:

  • what the entire code is doing

  • what is supposed to do

  • how you might fix it

  • anything you didn’t originally understand about the code (along with an explanation of what you learned about the confusing code)

Do This - Erase this and answer the questions from the cell above.

✅ Do This: Now that you know what’s wrong with the code, fix it! Edit the file with your solution and then re-run the code to make sure it works as intended.


5. Modifying another Python script, but this time with the vi text editor#

OK, so now that you’ve explored a Python script and have begun to get comfortable with running such a script on the command line, you’re going to download another script, but instead of editing the file withing the Jupyter text editor, you’ve doing to modify the script using a command-line-based text editor that is widely available on many systems: “vi

For this part of the activity, you’re going to work with a script that is designed to calculate the \(x\)-position and \(y\)-position of a projectile as a function of time. Since you are not expected to know the equations for projectile motion off the top of your head for this class, there are provided here:

\[x(t) = v_{0}t\cos(\theta)\]
\[y(t) = v_{0}t\sin(\theta) - \frac{1}{2}gt^2\]

where \(v_0\) is the magnitude of the initial velocity in meters per second, \(\theta\) is the angle that that the projectile is launched at in radians, \(g\) is the acceleration due to gravity in meters per seconds-squared, and \(t\) is the time in seconds.

You do not need to understand the physics behind projectfile motion to complete this activity, you only need to understand what the code is doing so that you can modify it a bit.

✅ Do This: Download the following script in your scripts directory using the command line:

https://raw.githubusercontent.com/msu-cmse-courses/cmse202-F22-data/main/code_samples/projectile_motion.py

Once you’ve downloaded it, test it out by running it on the command line. You should find that when the script runs, it makes an image called x_vs_time.png. You can find this image by looking for it on your Jupyter “home page”. It should look something like this:

x-vs-time

✅ Do This: Once you have the script downloaded and have confirmed that it works as intended, make a copy of it using the command line and call this new file projectile_motion_2.py (clever name, no?). What was the command you used to make the copy of the original?

Do This - Put the command you used to copy the file here.

Opening the file with vi#

✅ Do This: Using the vi directions from the PCA, let’s try opening the copy of the original script and making a few minor changes. Do the following:

  • Open projectile_motion_2.py using the vi command on the command line

  • Make the following changes to the script:

    • Change the call to the compute_trajectory function in the main function so that the velocity of projectile is 750 m s \(^{-1}\) and the launch angle is 45 degrees. Do not alter the compute_trajectory function itself.

  • Save and quit the file.

  • Test out your new function by running the script on the command line. If everything went as planned, you should have a new plot of \(x\) as a function of time that looks like this (indicating that the projectile traveled farther):

x-vs-time

Can we do something a little more interesting?#

Looking at a straight line isn’t all that much fun. It would be more interesting to see how the project traveled vertically since it should start out going up, but eventually slow down and change direction under the force of gravity. Wouldn’t it be nice if our script produced \(y\) as a function of time?

✅ Do This: Using vi, make a new function that plots y as a function of time. You should be able to use the pre-existing plot_x_vs_time as a basis for your new function. While this can be a little tedious if you’re just using the insert mode in vi, there are some additional vi commands that can make this a little eaiser. Try out the following:

  • Once you open the file in vi, move your cursor to the front of the line where def plot_x_vs_time is. Then press v and then move your cursor down until the entire function is highlighted.

  • Once the entire function is highlighted, press y for “yank” (copy) and then move your cursor down to a blank line below the plot_x_vs_time function and press p for “paste”. You should find that a duplicate version of the new function exists. You can now use insert mode to modify that function!

There’s a lot that vi can do once you start learning some of the commands and there are a lot of resources available for learning the commands (like this simple cheat sheet).

In your new version of the Python script, make sure you call your new function inside the main function and ensure that it saves a new file called y_vs_time.png. You should get something that looks like this:

y-vs-time

Much more interesting!


6. Testing out command line commands from inside a Jupyter notebook#

No that you’ve started to get comfortable with navigating the command line interface and editing files. Wer’e gonna spend the last bit of this assignment visiting how some of the same commands can be run from inside the Jupyter notebook itself.

Using “!#

One way to access command line commands from inside your jupyter notebook is to use the exclamation point/bang symbol, !. This symbol actually tells the Jupyter notebook to run the command on whatever the underlying default command prompt is for the computer where the Jupyter notebook is being run. In the case of JupyterHub, this is the “Terminal” that we’ve already talked a bit about, but if you’re running the Jupyter notebook on you computer locally, it may be something different, so be aware that this behavior may change depending on what computer/system you’re using!

✅ Do This: Try running the following cell to see what happens. Do the results make sense?

!pwd
!ls

✅ Do This: Carefully try executing some of the other command line commands you’ve learned using !. As always, make sure you know what you expect the command to before you run it! Did all of the commands you tried work? Which, if any, didn’t? You may want to experiment with using more than one cell.

# Test out some "!" commands here

Using the built-in IPython command line commands#

Because Jupyter notebooks are also IPython notebooks you can run some of the same commands that are available to you when you’re using the IPython command prompt.

✅ Do This: Run the following two cells. Do the results makes sense?

pwd
ls

✅ Question: What happens if you combined the above to commands into one cell? Does it work? If not, why not? What is happening with how the Jupyter notebook is interpreting the information?

# Test out putting both commands in the same cell here

✅ Do This: Experiment with other commands to determine which ones work and which ones don’t. Proceed with caution!

# Experiment here and create cells as necessary

Time permitting#

If you find yourself with extra time available and you haven’t yet tried getting all of this to work on your own computer (rather than JuypterHub), visit the sofware setup guide that is linked on the course website and give it a shot. If you run into issues, or you’ve already tried and run into issues, ask an instructor to take a look!


Congratulations, you’re done!#

Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for today’s assignment submission folder. (Don’t forget to add your name in the first cell!).

© Copyright 2023, The Department of Computational Mathematics, Science and Engineering at Michigan State University.