Pre-Class Assignment: Reflection and an introduction to the Command Line Interface (CLI)#

Day 2#

CMSE 202#

Learning the basics of the using the Terminal on the Engineering Jupyterhub Server#

✅ Put your name here

Learning goals for today’s pre-class assignment#

By the end of this pre-class assignment, you should:
  1. Reflect on your previous experiences with programming

  2. Remind yourself of the basics of Jupyter notebooks and (re)familiarize yourself with using the Engineering JupyterHub server.

  3. Be able to describe and use a Command Line Interface (CLI) to run simple Unix commands and Python code.

Assignment instructions#

Pre-class assignments will be composed of a combination of videos, text to read, and small assignments. The goal of these assignments is to prepare you for class the following day. You should watch the videos and read the text, and then do the assigned work. You will be graded on making a good-faith effort, not on absolute correctness! When you don’t understand something, make a note of it in the survey at the end of the notebook so that the instructor can work to address these issues.

Remember, to make notebook cells that have Python code in them do something, hold down the ‘shift’ key and then press the ‘enter’ or ‘return’ key (you’ll have to do this to get videos to run). To edit a cell (to add answers, for example) you double-click, add your text, and then enter it by holding down ‘shift’ and pressing ‘enter’.

This assignment is due by 11:59 p.m. the day before class, and should be uploaded into the appropriate “Pre-Class Assignments” submission folder on D2L. You should also make sure to provide thoughtful answers to the survey at the end of this assignment as this will also be used to ensure you get credit for your work.


1. Reflecting on your experiences with programming up to this point#

As you start CMSE 202, now is a good time to relfect on your prior programming experiences. Answer the following questions as you think back on what you’ve learned in the past about how to write and use code, either inside or outside the classroom.

Question 1: Besides Python, what other languages have you been introduced to (ex. C, C++, R, FORTRAN, MATLAB, JS, etc). How comfortable are you using these other languages? Also comment on how you feel about your ability to write Python code. What do you feel like you have good mastery of in Python and where do you think you have room for improvement?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text). Please use appropriate Markup formating to make your answer’s easy to read.

Question 2: Besides Jupyter (the interface you’re using to access this notebook), What other programming interfaces have you used (if any)? Some examples may include; Command line interfaces, Python Spyder, Microsoft Visual Studio, Eclipse, etc. What do you like/dislike about each of these interfaces, Jupyter included?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Question 3: Think back on everything you’ve done that has involved programming up to this point, provide an example of something you did that you were particularly proud of or that you thought was particularly neat/interesting. In addition, reflecting on something you might have been less proud of, provide an example of something that you think you would do differently given the abilities you have now or could have done a better job with.

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


2. Reminding yourself how Jupyter/JupyterHub works#

The following video will review the Python Jupyter interface and the Engineering JupyterHub server. Take this as an opportunity to reminder yourself how these tools work and what they do.

In addition, make sure you can log onto the Engineering Jupyterhub account using the of following link:

In order to log into the JupyterHub you need to activate your DECS account first. If you haven’t done this yet, go to:

Your “Initial Password” is your PID, which is the number that starts with “A” on your MSU SpartanCard, followed by the “@” symbol (e.g. A123467890@). Once you activate your account, you’ll be able to log in to JupyterHub. Contact the instructor if you have issues with this process.

Note: In this course, the assumption will be that you have access to and are familiar with using the JupyterHub server, but if you prefer to use your local Python installation for the majority of the work in this course, you are welcome to do so. However, there may be times when JupyterHub is required to ensure that all of the software will work as intended. When you run into software issues with your local installations, the instructors will do their best to help you troubleshoot these issues, but if they prove particularly troublesome, you may need to revert to JupyyterHub as a backup. For this pre-class notebook, you will be expected to use the JupyterHub for all of the command-line interface (CLI) components to get practice with it. After you practice with the command line interface in JupyterHub, you are encouraged to see if you can do the same thing locally on your computer by following the recommendations for setting everything up in the Software Setup Guide (available on the course website. Being able to run everything locally has the advantage of not relying on internet access, but it will not be required for you to succeed in the course.

# the commands below this comment imports the YouTube Video module and then displays
# a specific YouTube video, with a given width and height.  You can watch the video
# in full-screen mode, which can be easier to see, by clicking the little box in the
# bottom-right corner of the video.

from IPython.display import YouTubeVideo
YouTubeVideo("5WSQnGmz3IA",width=800,height=450)

If the above YouTube video doesn’t work, you can try the MediaSpace version.

Question 4: Is there anything about using Jupyter or the JupyerHub server that you’re still feeling unsure about?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)


3. What is a Command Line Interface (CLI)?#

First off, in order to work through this part of this assignment, you will need to use the JupyterHub interface. If you’re already running this notebook there – you’re all set! If you have been working on this notebook on your local Python installation up to this point, you should save it, and then upload it to the JupyterHub and continue working on it there. You should can log onto the Engineering Jupyterhub account using the following link:

At the end of the assignment, you’ll need to download the notebook from JupyterHub before you can upload it to D2L.

Exploring the command line interface#

The following video will introduce you to the Engineering Jupyterhub Terminal program which is one possible Command Line Interface (CLI) that can be used for work done in this class. I highly recommend that you follow along and practice executing the same commands as the video. If you are watching/listening to the video on-the-go, a summary of the commands presented in the video are included below as a reference for when you get back to your computer.

from IPython.display import YouTubeVideo
YouTubeVideo("ENlkdE8pIh8",width=800,height=450)

If the above YouTube video doesn’t work or you want a higher resolution version, you can try the MediaSpace version.

Question 5: Now that you’ve have a chance to learn a bit about the commandline interface, review the commands below and add a short description for each of them.

Do This - Modify the following list to include short (just a few words) descriptions for each command as it was presented in the video. A few descriptions are provided for you as examples and some of them are repeated:

  • clear:

  • pwd:

  • ls:

  • cd junk: Change to the directory named junk

  • cd ..:

  • cd ~:

  • cd -: Change to the previous directory

Question 6: What is the name of the command for creating a new directory in the terminal program?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

File manipulation and running python code from the command line#

Now that you’ve had a chance to explore how to look around and move around your computer on the command line, let’s take a look at how we can create, copy, and move files using the command line.

Watch the following video and try to follow along by experimenting your own command line interface.

from IPython.display import YouTubeVideo
YouTubeVideo("ujGqavjFqN0",width=800,height=450)

If the above YouTube video doesn’t work or you want a higher resolution version, you can try the MediaSpace version.

Question 7: Now that you’ve had a chance to learn a bit more about the commandline interface, review the commands below and add a short description for each of them.

Do This - Modify the following list to include short (just a few words) descriptions for each command as it was presented in the video.

  • touch: (it might be worth doing an internet search for “touch unix command” for this one to unpack a bit more about what it does)

  • cp:

  • mv:

  • rm:

  • less:

  • more:

Question 8: If I have a Python script called compute_average.py and I wanted to execute script from the command line, what would be the command necessary to do that?

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

Using the Python and/or IPython Command Prompts#

OK, so you’re starting to learn a bit about how to use the command line to interact with your computer, manipulate files, and run Python scripts. But what if we want to just run some python code one line at a time inside of the command line interface? Perhaps we just want to fiddle with a code idea we have, or maybe we need to run a few simply calculations that don’t warrant using a Jupyter notebook or a script?

Watch the following video to learn how you can run Python from the command line using the Python and IPython prompts.

from IPython.display import YouTubeVideo
YouTubeVideo("HLMgisd44C0",width=800,height=450)

If the above YouTube video doesn’t work or you want a higher resolution version, you can try the MediaSpace version.

Question 9: Now that you’ve had a chance to learn a bit about how to use the Python and IPython prompts, review the commands below and add a short description for each of them.

Do This - Modify the following list to include short (just a few words) descriptions for each command (not all of which were presented in the video). If you’re unsure about any of the commands, test them out on the command line!

  • python:

    • import random:

    • random.randint(1,100):

    • exit():

  • ipython

    • import numpy as np:

    • np.linspace(0,100,5):

    • ls:

    • pwd:

    • cd:

    • exit():

Nice work!#

Hopefully you’re starting to understand how you can interact with computers using the command line in a variety of way. We’ll be exploring this more in class!


4. Opening Files with vi#

Before there was Google Docs or even Microsoft Word, programmers needed a simple way to write a text file–a text editor. Previously, all code was written on physical punch cards; dynamically editing code/text stored in the computer using the computer itself would be a gian leap forward in terms of human-computer interaction. This is the world in which vi was born. Its functionality was bare-bones, but it did everything those early software engineers needed.

IMPORTANT: Make sure to read the following content; vi it’s the commands take a little getting used to, but it is an incredibly useful tool.

  • To open a file in vi on the command line, you need to type “vi [file]” into the CLI, which will open the file and dump you into the vi text editor

  • When you first open vi, you will not be able to immediately type things to make modifications like you might be used to. Instead, you will start out in the vi “command mode”, which allows you to manipulate the file using specific letter-based commands.

  • To enter the text-editing mode you’re familiar with, you need to press i which puts you in “insert mode”. From here, you can type normally.

  • When you are done typing in insert mode, you need to press ESC, which takes you back to command mode.

  • If you’re happy with everything you’ve done, you can type :wq, which will “write” (w) and then “quit” (q). If you ever want to quit without saving your changes, you can use :q!.

  • For more vi commands, check out this cheat sheet

Question 10: Use the directions above to make a new file, put some text in it, and save it. Specifically, do the following:

  1. Open a new file called new_file.txt using vi.

  2. Enter the Insert mode (the command for which is given above).

  3. Write some text into the file.

  4. Write the contents of your new file and quit vi (the commands for which are given above).


Follow-up Questions#

Copy and paste the following questions into the appropriate box in the assignment survey include below and answer them there. (Note: You’ll have to fill out the section number and the assignment number and go to the “NEXT” section of the survey to paste in these questions.)

  1. Are there any aspects of programming in Python that you feel like you still struggle with from time to time?

  2. Have you ever used a “Command Line Interface” before? If so, what did you use it for?

  3. If you’ve tried to get the command line interface working on your computer locally, have you succeeded? Are you able to run all of the same commands on your computer as were run in the videos above? If not, what are you having issues with? [Note: It is not absolutely necessary to get the command line interface working on your computer, but it can be handy to have as an option].


Assignment wrap-up#

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://cmse.msu.edu/cmse202-pc-survey" 
	width="800px" 
	height="600px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, you’re done with your first pre-class assignment!#

Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for today’s 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