Skip to content

Commit

Permalink
Merge pull request #14 from mikhailklassen/ptwobrussell-patch-1
Browse files Browse the repository at this point in the history
Remove use of Envoy and Bash magic from App C
  • Loading branch information
mikhailklassen authored Feb 26, 2019
2 parents ffe04f3 + 28642ba commit 743a9aa
Showing 1 changed file with 0 additions and 89 deletions.
89 changes: 0 additions & 89 deletions notebooks/_Appendix C - Python & Jupyter Notebook Tips.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -489,95 +489,6 @@
"print(\"Hello, {you}. My name is {me}\".format(**names))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interact with the Virtual Machine without an SSH Client Using Python\n",
"Although you're probably better off to configure and use an SSH client to login to the virtual machine most of the time, you _could_ even interact with the virtual machine almost as though you are working in a terminal session using Jupyter Notebook and the [envoy](https://github.com/kennethreitz/envoy) package, which wraps the [subprocess](https://docs.python.org/3.7/library/subprocess.html) package in a highly convenient way that allows you to run arbitrary commands and see the results. The following script shows how to run a few remote commands on the virtual machine (where this Jupyter Notebook server would be running if you are using the virtual machine). Even in situations where you are running a Python program locally, this package can be of significant convenience."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import envoy # pip install envoy\n",
"\n",
"# Run a command just as you would in a terminal\n",
"r = envoy.run('ps aux | grep jupyter') # show processes containing 'jupyter'\n",
"\n",
"# Print its standard output\n",
"print(r.std_out)\n",
"\n",
"# Print its standard error\n",
"print(r.std_err)\n",
"\n",
"# Print the working directory for the IPython Notebook server\n",
"print(envoy.run('pwd').std_out)\n",
"\n",
"# Try some commands of your own..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bash Cell Magic\n",
"\n",
"An alternative to using the envoy package to interact with the virtual machine through what is called \"Bash Cell Magic\" in IPython Notebook. The way it works is that if you write <code>%%bash</code> on the first line of a cell, IPython Notebook will automatically take the remainder of the cell and execute it as a [Bash](http://tldp.org/LDP/abs/html/) script on the machine where the server is running. In case you come from a Windows background or are a Mac user who hasn't yet encountered Bash, it's the name of the default shell on most Linux systems, including the virtual machine that runs the IPython Notebook server.\n",
"\n",
"Assuming that you are using the virtual machine, this means that you can essentially write bash scripts in IPython Notebook cells and execute them on the server. The following script demonstrates some of the possibilities, including how to use a command like <code>wget</code> to download a file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"\n",
"# Print the working directory\n",
"pwd \n",
"\n",
"# Display the date\n",
"date\n",
"\n",
"# View the first 10 lines of a manual page for wget\n",
"man wget | head -10\n",
"\n",
"# Download a webpage to /tmp/index.html\n",
"wget -qO /tmp/foo.html http://ipython.org/notebook.html\n",
"\n",
"# Search for 'ipython' in the webpage\n",
"grep ipython /tmp/foo.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using Bash Cell Magic to Update Your Source Code\n",
"\n",
"Since Bash cell magic works just as though you were executing commands in a terminal, you can use it easily manage your source code by executing commands like \"git status\" and \"git pull\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"ls ../\n",
"# Displays the status of the local repository\n",
"git status\n",
"\n",
"# Execute \"git pull\" to perform an update"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 743a9aa

Please sign in to comment.