└── README.MD /README.MD: -------------------------------------------------------------------------------- 1 | # How to Download Udacity Workspace 2 | 3 | ## Download Jupyter workspace 4 | 5 | To download all files in a Jupyter workspace: 6 | - Go to _File > New Notebook > Python 3_ - this will create a new Jupyter 7 | Notebook. 8 | - Paste the [code snippet 9 | below](#download-snippet) into the code cell in the new Jupyter Notebook 10 | - Click on Jupyter logo (top left of the screen) to open the directory browser. 11 | This will show all files and directories in the workspace. 12 | - Find `workspace_archive.tar`. download it. 13 | 14 | ### Download extra data located outside of current workspace 15 | 16 | Occassionally, you'll see in the code of a Notebook where it gets data from 17 | a different directory than the current workspace. In other words, the 18 | data is located outside of current `./` working directory. 19 | 20 | For example, the code below is trying to get data from 21 | `../../data`: 22 | 23 | ```python 24 | def example(): 25 | data = os.path.join(os.getcwd(), '..', '..','data') 26 | return np.load(data) 27 | ``` 28 | 29 | To download the data, simply run `make_tar_file` function from the [code snippet 30 | below](#download-snippet) with `dir_name` being the path to the data: 31 | 32 | ```python 33 | dir_name = os.path.join(os.getcwd(), '..', '..','data') 34 | make_tar_file(dir_name) 35 | ``` 36 | 37 | ## Download Python workspace 38 | 39 | For Python workspaces, use the [code snippet 40 | below](#download-snippet) but put it in a Python file instead. 41 | 42 | ## Download Snippet 43 | 44 | The snippet below compresses all files in a workspace and put them in 45 | `workspace_archive.tar` file. 46 | 47 | ```python 48 | """ Source: 49 | - https://stackoverflow.com/questions/48122744/how-to-download-all-files-and-folder-hierarchy-from-jupyter-notebook 50 | """ 51 | import os 52 | import tarfile 53 | 54 | def recursive_files(dir_name='.', ignore=None): 55 | for dir_name,subdirs,files in os.walk(dir_name): 56 | if ignore and os.path.basename(dir_name) in ignore: 57 | continue 58 | 59 | for file_name in files: 60 | if ignore and file_name in ignore: 61 | continue 62 | 63 | yield os.path.join(dir_name, file_name) 64 | 65 | def make_tar_file(dir_name='.', target_file_name='workspace_archive.tar', ignore=None): 66 | tar = tarfile.open(target_file_name, 'w') 67 | 68 | for file_name in recursive_files(dir_name, ignore): 69 | tar.add(file_name) 70 | 71 | tar.close() 72 | 73 | 74 | dir_name = '.' 75 | target_file_name = 'workspace_archive.tar' 76 | # List of files/directories to ignore 77 | ignore = {'.ipynb_checkpoints', '__pycache__', target_file_name} 78 | 79 | make_tar_file(dir_name, target_file_name, ignore) 80 | ``` --------------------------------------------------------------------------------