Skip to main content
All CollectionsCoursesCourse Development
IDE Exercises: Baking files in the image
IDE Exercises: Baking files in the image
George Boorman avatar
Written by George Boorman
Updated over a week ago

Only the files you expect students to edit, or you expect Content to have to maintain, should be added through the Teach editor. Others should be added directly to the course image, and imported on a need-to-use basis in the PEC.

For example, as you build a course using IDE exercises you may have to let students access a dataset file throughout the course. You don’t want to have multiple files to scroll through in Teach, or have to copy data directly to the IDE, as that would make the exercise edition cumbersome.

Additionally, if your exercises build on top of each other and students complete a project as they progress through the course, you don't want all of the final resources to be available to the student from the beginning.

The solution is to hide the resources you need somewhere the student does not have access, and cherry pick what you need for each exercise. It’s also especially useful to copy datasets instead of creating them directly in Teach.

Copying files and directories to the student workspace

Students start in the workspace directory, so all necessary resources should be copied to this directory. Resources can be stored directly on the image. You will need to store them in the /tmp directory.

To do this, create a /dependencies folder and within it an /assets directory for files and directories used on the whole course, and a directory for each chapter with a subdirectory for each exercise for which you need to copy data or scripts:

/
+-- bin
+-- boot
+-- dev
+-- etc
...
+-- tmp
| +-- dependencies
| +-- assets
| +-- dataset.csv
| +-- article.txt
| +-- image.jpg
| +-- c1
| +-- e01
| +-- importing
| +-- importing.py
| +-- processing
| +-- processing.py
| +-- helpers.py
| +-- c2
| +-- c3
| +-- c4

Create this dependencies folder, with what it should contain, zip it, and upload it to Assets via Teach.

Then, on the course repository on GitHub, edit requirements.sh to download the zipped file and bake it in the Docker image:

### BAKING
# Update packages and install unzip package
apt-get --quiet=2 update && apt-get --quiet=2 --assume-yes install unzip zip screen

# Create a function to unzip dependencies hosted on S3 with Assets
# Called using `get_unzip() link directory_name`
get_unzip () {
# $1 grabs the first element following the function call
# (the link to the zipped directory)
# $2 grabs the second elements following the functions call
# (the name of the directory hosting the unzipped directory
wget -O /tmp/package.zip $1
unzip -o /tmp/package.zip -d /tmp/$2
chmod 777 -R /tmp/$2
rm /tmp/package.zip
}

# Bake dependency zip to the course image
get_unzip https://assets.datacamp.com/.../dependencies.zip dependencies

### HOUSEKEEPING
# Make sure that when a user runs python, they are using python3
echo "alias python='''python3'''" >> ~/.bashrc

# Prevent pip from offering to update to the latest version
echo "export PIP_DISABLE_PIP_VERSION_CHECK=1" >> ~/.bashrc

# Make a backup copy of bashrc
cp ~/.bashrc /usr/local/share/bashrc_backup

Now the files are available on the image, so you can access them from the PEC section or the exercise console in case you need to do some exploration. In the PEC section of your IDE exercise, you can then copy these resources from their directory on the virtual machine to the workspace as required for each exercise, using shutil.copytree():

# Import the necessary libraries
import shutil

# Select the source folder where the material is stored
source = "/tmp/dependencies/c1/e01/"

# Select the destination folder
destination = "/home/repl/workspace/"

# Copy the source folder to the destination
shutil.copytree(source, destination)

If you want to copy files instead of folders, you can use shutil.copyfile() instead of shutil.copytree():

# Import the necessary libraries
import shutil

# Select the source folder where the material is stored
source = "/tmp/assets/dataset.csv"

# Select the destination folder
destination = "/home/repl/workspace/data/dataset.csv"

# Copy the source folder to the destination
shutil.copyfile(source, destination)

Content persistence across exercises

The state of the environment persists from one exercise to the other. If you need students to start fresh from the previous exercise, check the Cleaning up environment at exercise initialization section of this article.

Did this answer your question?