Skip to content

Instantly share code, notes, and snippets.

@jonathanmorgan
Last active January 12, 2017 11:31
Show Gist options
  • Save jonathanmorgan/555be4d237e85eb22385 to your computer and use it in GitHub Desktop.
Save jonathanmorgan/555be4d237e85eb22385 to your computer and use it in GitHub Desktop.
Directory help.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:ad97c4de71294b5702676f33598b94cb887023e690b6919f6db79c85eba1dbe3"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Working with directories"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Make a Work Directory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, you should make a directory in which you'll do all your work for a given project or class. Choose a location that is in your user's profile directory, so you are sure you'll have read and write access to it.\n",
"\n",
"My pattern is to make a \"work\" directory inside \"Documents\" (mac) or \"My Documents\" (windows), then create folders in there for individual projects. A location in Documents is easy to get to when you are saving files from programs or a web browser, and since the programs we are using default to opening in your user's home directory, you make it more likely that you'll be able to find your work directory in other programs when they open."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"github repo directory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I'd place your github repository in your work directory, so you know where everything is, and such that getting to your work directory is the first step for getting to most of your work. Get there, then move to the place inside that... is your git repo, for example."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"getting a shell to a directory you see in Finder or Explorer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you have trouble finding a file that you have downloaded and are trying to open in ipython or ipython notebook, it can be easier to look for it in a GUI file finding program like Finder or Explorer than to use the command line. Once you find the file, however, you need to get python to that directory. Here are some tips to make that easier in Windows and Mac."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Windows"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- In Windows, there is an item named \"Open command window here\" that you can get to by shift-right-clicking on a folder in Explorer. To do this:\n",
"\n",
" - click on a folder in Explorer to select the folder.\n",
" - then, hold down the shift key while right-clicking on the folder.\n",
" - A context menu should appear that has the command \"Open command window here\" in the middle of its list of commands.\n",
" - Click \"Open command window here\" to get a Windows command window opened that is already in the directory you selected. This isn't bash, but you should be able to run `ipython` or `ipython notebook` from this command prompt."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Mac"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- In the Mac, there is a similar feature you can set up through OS X services. To configure:\n",
"\n",
" - In Finder, click on a folder, then go to menu bar at top and click Finder --> Services -> Services Preferences. This opens Settings --> Keyboard --> Shortcuts (obvious, right?).\n",
"\n",
" - \"Services\" should be selected on the left. In the right, under Files and Folders, make sure that \"New Terminal at Folder\" and \"New Terminal Tab at Folder\" are checked.\n",
"\n",
"- Then, in Finder, if you want to open a terminal at a given folder:\n",
"\n",
" - Right click on the folder.\n",
" - In context window that opens, go down to Services (at the bottom) and hover over it to open the services menu.\n",
" - In Services, click \"New Terminal at Folder\" to open the folder in a new terminal window, or \"New Terminal Tab at Folder\" to open it in a tab in an existing Terminal window.\n",
"\n",
"- Also, Finder \"View\" preferences are your friend. With Finder as the active application:\n",
"\n",
" - Path Bar - If you select \"View --> Show Path Bar\", Finder will display the full path, directory-by-directory, to the currently selected file or folder in Finder in a bar at the bottom fo the Finder window.\n",
" - Status Bar - If you select \"View --> Show Status Bar\", Finder willi display status at the very bottom of the Finder window, even under the Path Bar. This information can be different based on what is selected, but in general it tells information about the currently selected file(s) and director(y/ies) including size and number of files if more than one are selected."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Getting to a directory in an existing shell"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you don't know the full path, use Finder or Explorer to find the file, then open a command window or terminal, then in Windows, type \"dir\", and in bash, type \"pwd\". Dir lists contents of directory, but at top, it outputs full path to directory (that should just be in the prompt here, too, but it might truncate if the path is too long). Pwd just outputs current directory.\n",
"\n",
"Then, in shell or in straight up IPython interactive session, you can just do:\n",
"\n",
" cd <full_path_to_directory>\n",
"\n",
"- On Windows, you'll leave off the drive letter - so if your path is \"C:\\Users\\jmorgan\\Documents\", you'd do \"cd \\Users\\jmorgan\\Documents\".\n",
"\n",
"In IPython notebook, create a new code cell, place the cd command in the code cell, then run the code cell. Example:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%cd /Users/awesome/Documents/work"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Downloading and running an IPython Notebook linked from CTools"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Open the IPython Notebook in your browser.\n",
"- Download the actual source (not a \"web archive\") to your working directory.\n",
"\n",
"then, depending on how you want to interact with the notebook:\n",
"\n",
"- through shell\n",
"\n",
" - open shell to the folder that contains the IPython notebook.\n",
" - run \"ipython notebook\"\n",
" - click on the file name.\n",
"\n",
"- through anaconda launcher:\n",
"\n",
" - open ipython notebook\n",
" - navigate to the directory where you downloaded the notebook.\n",
" - click on the file."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%nbtoc"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<!-- extracted from https://gist.github.com/magican/5574556 -->\n",
"<div id=\"toc-wrapper\">\n",
" <div class=\"header\">Contents <a href=\"#\" class=\"hide-btn\">[hide]</a></div>\n",
" <div id=\"toc\"></div>\n",
"</div>\n",
" \n",
"<style>\n",
" #toc {\n",
" overflow-y: scroll;\n",
" max-height: 300px;\n",
" }\n",
" #toc-wrapper {\n",
" position: fixed; top: 120px; max-width:430px; right: 20px;\n",
" border: thin solid rgba(0, 0, 0, 0.38); opacity: .8;\n",
" border-radius: 5px; background-color: #fff; padding:10px;\n",
" z-index: 100;\n",
" }\n",
" #toc-wrapper.closed {\n",
" min-width: 100px;\n",
" width: auto;\n",
" transition: width;\n",
" }\n",
" #toc-wrapper:hover{\n",
" opacity:1;\n",
" }\n",
" #toc-wrapper .header {\n",
" font-size:18px; font-weight: bold;\n",
" }\n",
" #toc-wrapper .hide-btn {\n",
" font-size: 14px;\n",
" }\n",
" \n",
"</style>\n",
"\n",
"<style>\n",
" ol.nested {\n",
" counter-reset: item;\n",
" list-style: none;\n",
" }\n",
" li.nested {\n",
" display: block;\n",
" }\n",
" li.nested:before {\n",
" counter-increment: item;\n",
" content: counters(item, \".\")\" \";\n",
" }\n",
"</style>\n"
],
"metadata": {},
"output_type": "display_data"
},
{
"javascript": [
"// adapted from https://gist.github.com/magican/5574556\n",
"\n",
"function clone_anchor(element) {\n",
" // clone link\n",
" var h = element.find(\"div.text_cell_render\").find(':header').first();\n",
" var a = h.find('a').clone();\n",
" var new_a = $(\"<a>\");\n",
" new_a.attr(\"href\", a.attr(\"href\"));\n",
" // get the text *excluding* the link text, whatever it may be\n",
" var hclone = h.clone();\n",
" hclone.children().remove();\n",
" new_a.text(hclone.text());\n",
" return new_a;\n",
"}\n",
"\n",
"function ol_depth(element) {\n",
" // get depth of nested ol\n",
" var d = 0;\n",
" while (element.prop(\"tagName\").toLowerCase() == 'ol') {\n",
" d += 1;\n",
" element = element.parent();\n",
" }\n",
" return d;\n",
"}\n",
"\n",
"function table_of_contents(threshold) {\n",
" if (threshold === undefined) {\n",
" threshold = 4;\n",
" }\n",
" var cells = IPython.notebook.get_cells();\n",
" \n",
" var ol = $(\"<ol/>\");\n",
" $(\"#toc\").empty().append(ol);\n",
" \n",
" for (var i=0; i < cells.length; i++) {\n",
" var cell = cells[i];\n",
" \n",
" if (cell.cell_type !== 'heading') continue;\n",
" \n",
" var level = cell.level;\n",
" if (level > threshold) continue;\n",
" \n",
" var depth = ol_depth(ol);\n",
"\n",
" // walk down levels\n",
" for (; depth < level; depth++) {\n",
" var new_ol = $(\"<ol/>\");\n",
" ol.append(new_ol);\n",
" ol = new_ol;\n",
" }\n",
" // walk up levels\n",
" for (; depth > level; depth--) {\n",
" ol = ol.parent();\n",
" }\n",
" //\n",
" ol.append(\n",
" $(\"<li/>\").append(clone_anchor(cell.element))\n",
" );\n",
" }\n",
"\n",
" $('#toc-wrapper .header').click(function(){\n",
" $('#toc').slideToggle();\n",
" $('#toc-wrapper').toggleClass('closed');\n",
" if ($('#toc-wrapper').hasClass('closed')){\n",
" $('#toc-wrapper .hide-btn').text('[show]');\n",
" } else {\n",
" $('#toc-wrapper .hide-btn').text('[hide]');\n",
" }\n",
" return false;\n",
" })\n",
"\n",
" $(window).resize(function(){\n",
" $('#toc').css({maxHeight: $(window).height() - 200})\n",
" })\n",
"\n",
" $(window).trigger('resize')\n",
"}\n",
"\n",
"table_of_contents();\n",
"\n",
"\n"
],
"metadata": {},
"output_type": "display_data"
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment