Toolbox talk: Atom, Jupyter and Hydrogen

Toolbox talk: Atom, Jupyter and Hydrogen

As an early-career researcher, I am still figuring out the best way to get stuff done. Every day it seems like my to-do list gets longer and longer, for every item I tick off two more take its place and I have no doubt it will only get worse. I am all about finding the best, most efficient and connected technologies to help alleviate some unnecessary legwork. I have decided to write more on the programs, apps and integrations that I use every day in this Toolbox series. This post focuses on what is currently the best (in my humble opinion) hackable text editor for writing, programming and data analysis: Atom!

The basics: Atom as a building block.

Billed as the “hackable text editor for the 21st Century”, Atom is a free, open-source, cross-platform desktop application for editing plain text and source code. While the base program was developed by GitHub, many of best functionalities are community built and maintained under free liscences. This combination of core support and extensibility has resulted in an unparalleled level of customisability, and makes Atom my editor of choice for everything from simple text documents, to data analysis to developing python packages. For coders and non-coders alike, there is something for everyone in the Atom ecosystem.

From an aesthetic point of view, Atom has a nice panelled interface which can be extended at will. Below you can see a standard layout I gravitate towards while I am analysing data. The leftmost pane (the file tree) allows you to open an entire folder or folder tree to easily see all of the files associated with the project you are currently working on, complete with coloured file-type icons for easy visual grepping. In addition, I can have multiple panes open side-by-side which allows me to operate on code/data in one building and document the analysis approach in the second. Finally, to the right is the output/Git pane (more on that later).

An example analysis layout in Atom.

Atom also has a vast array of installable themes to customise the window colour and aesthetic. I myself am partial to a dark theme, with a subdued syntax highlighting (check out the OneDark theme). If you prefer a more traditional light theme, there are plenty of those too!

The best bits: Git(Hub), Integrated Development Environment and Real-time Collaboration functionality

Being built by GitHub, it is fair to expect some level of integration with Git. If you’re not yet familiar with Git, or version control in general, you can read more about it here. In short, even if you have never written a single line of code in your life, version control is a must for anyone routinely creating and editing documents of any kind. For now, suffice to say that Git has traditionally been a command-line tool and therefore mildly intimidating to those of us who found it with little experience. Recently, GitHub introduced a desktop application which alleviates some of the hassles of interacting with Git via the command line. However, Atom takes this functionality and seamlessly integrates it within the environment where the work is being done. No longer do I need to switch windows to another app or terminal – I can immediately see the documents that have detected changes, stage and commit those changes and push to GitHub from within Atom. More than this, Atom allows you to create and change branches, and push/fetch/pull from remote repositories all without ever having to change windows.

One of the reasons I came to it in the first place was the flexibililty to create an Integrated Development Environment (IDE) for any programming language. An IDE typically provides comprehensive facilities to edit, build and debug source code for a given language. Atom has taken on this task to provide support for a long long list of the most popular languages. This is achieved via installation of community-driven packages – more on this in a little bit.

Finally, Atom provides an interface for real-time collaborative coding. While I am yet to test this functionality, Atom’s “Teletype” promises the ability for users to share their workspace with team members and collaborate on code in real time using a shared portal. If you have used this feature, be sure to let me know how you find it!

Easy extensions: Size may not matter, but it’s all about the package(s)

They don’t call Atom the most hackable text editor for nothing! Although the Git interface is packaged as part of the raw Atom install, many of the other features I’ve already described rely on the installation of community developed and maintained packages. The variety and functionality of these packages is seemingly endless, and installing a new package is as simple as opening the settings view and searching for the package. No matter what language you intend to use Atom for (or even just for general editing of plain text or markdown documents), there are bound to be a few packages that will make life easier.

Given Atom is agnotistic to language, there are plenty of packages for common languages like R, Java, C #…. If any of these is your language of choice, I encourage you to poke around online and see what packages people recommend. For the remainder of this post, I will focus on how I set up Atom as a home for my Python development and data analysis.

Accessorising Atom: a Python perspective

When first learning python, I used Spyder as my development environment. After a while, I got itchy feet and investigated Sublime (another text editor) and was intrigued by it’s customisability. After a brief stint, it wasn’t long before I discovered Atom and, as they say, the rest is history! There are plenty of generic “how-to-set-up-Atom-for-Python” articles (you can find examples here and here), so I won’t spend too much time rehashing these here. Suffice to say, there are a few basic packages you will want to install to get the most out of Atom for your python projects. A few examples include:

Perhaps my favourite aspect of Atom (and in fact the reason I know recommend it to everyone I help get started with python) is the easy integration with Jupyter. If you haven’t come across Jupyter yet, Jupyter Notebooks provide an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text in a whole host of languages. Notebooks support over 40 programming languages at the time of writing, including Python, R, Julia….This is managed through the installation of different kernels, and if you are keen to run additional languages then check out how to access different kernels here. In the case of python, IPython is the central dependency of Jupyter and thus is preinstalled. Jupyter Notebooks are in and of themselves a great environment for data cleaning and transformation, numerical simulation, statistical modelling, data visualization and much more. However, they require the analysis, version control and file management to be completed in different locations (other windows, apps, etc).
Enter Hydrogen. This neat package brings all the functionality of Jupyter notebooks (documentation, in-line plotting and results etc) to your atom environment using the installed kernels. In my opinion, this is a must have for someone getting started with Python in Atom. To get started, follow the installation guide here. As a bonus, hydrogen-python is an extension package providing variable exploration in beta. If this is an IDE feature you cannot live without, check it out!

Better than basics: integrating python virtual environments

Similar to Git, using virtual environments with Python (despite being important best practice!) can be extremely intimidating for a newbie. Again, having to make and activate virtual environments via the terminal, and keep track of potential leftmost to use, set the bar above what I could digest and integrate into my workflow initially. Without this essential tool, my base install of python became littered with packages, many of which had dependencies that broke other packages. And don’t even get me started on the nightmare that was needing a safe install of both Python 2 and Python 3 for different projects!

Eventually, I got my act together and installed both virtualenv and virtualenvwrapper. If you need a little more advice, check out these great tutorials from Real Python and Hitchhiker’s Guide to Python. For a little while, I was content that I could activate different environments if I desperately needed to – but was not overly inclined to use them on a regular basis! That was until I found the Atom package specifically designed to integrate this functionality: atom-python-virtualenv. This package lets you activate different virtual environments from within Atom, and combined with Hydrogen’s kernel manageraesthetic can have complete control of the python environment within which you are trying to run code!

Further Reading: Example Atom/Jupyter/Hydrogen/VirtualEnv Setup

As it happens, the inspiration for this post came from helping multiple new coders setup their Atom environment over the course of a few weeks. Using the resources I have shared above, I cobbled together a general use-case for installing these elements from scratch. If you already know the theory behind these components, then this guide should serve as a checklist to make sure each of the important elements are installed and have you up and running in no time!

Step 1: Checking your default Python install

Open a new terminal and check which version of python you are natively running using which python. Most commonly, this will be Python 2, and thus every virtual environment you create will have this as a default. Alternatively, to set the default to Python 3, use which python3, and note the directory address.

Step 2: Install virtualenv, virtualenvwrapper

In this terminal window, execute the following commands to install virtualenv and virtualenvwrapper using pip. NB: in my experience, MacOS has a weird way of installing things for the user, therefore include sudo with all pip commands to ensure they are installed to the generic directory.

sudo pip install virtualenv
sudo pip install virtualenvwrapper

Step 3: Configure virtualenvwrapper

Locate the .bash_profile file, and open in nano via nano .bash_profile. To the bottom of this file, append the following lines:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON=/path/to/your/prefered/python

Close nano using esc, :x, then reload the bash file using source .bash_profile.

To test virtualenvwrapper has been installed and configured correctly, run workon

Step 4: Create virtual environment

mkvirtualenv SOME_ENV

If desired, you can also direct virtualenv to a specific python exe to use (using the -p flag and full path to the relevant python exe)

mkvirtualenv -p /usr/local/bin/python3 SOME_ENV

Step 5: List, activate and deactive virtual environments

You should now be ready to create and use virtual environments from the command line. To test this, run workon and ensure the SOME_ENV is listed. To activate and deactivate this environment:

workon SOME_ENV

python #run python to check the version

exit() #exit python to return to command prompt

deactivate

Step 6: Install relevant packages for your new environment

Activate the environment of interest, then install any relevant packages using pip

pip install SOME_PACKAGES

Some useful PyPI examples include:

  • pandas
  • seaborn
  • matplotlib
  • xlrd
  • xlsx-writer
  • ipykernel

You can also install packages from directly from a git repository using the -e flag for editable (great for packages under development!).

Fork then clone a version of the repo to a known location on your desktop (git clone <URL-TO-REPO>), then install using pip:

python -m pip install -e <FULL_PATH_TO_REPO>

Step 7: Install new ipython, and configure a kernel from within the environment

Install a fresh version of jupyter and ipython to your environment, then create a new kernel from within the environment for atom to use. Here, I generally title the kernel the same as my virtualenv, making it obvious that they should be run concurrently for consistency.

pip install jupyter, ipykernel
ipython kernel install --user --name=SOME_ENV

You can then check the list of currently installed kernels, and should see your newly created kernel:

jupyter kernelspec list

Step 8: Install Atom packages

Open Atom and enter the Settings view to install atom-python-virtualenv and hydrogen.
After installing hydrogen, restart Atom to detect the installed kernels.

Step 9: Using the virtualenvwrapper package in Atom

Create a test.py file. In the bottom status bar, click the “no virtualenv” section, and select SOME_ENV. In the test file, attempt to import a newly-installed package and run the line using hydrogen (position the cursor within the import line, then ctrl-enter). Atom should then prompt you to select the appropriate kernel via a pop-up menu, then process the code. If everything is configured correctly, the line should run without error!

Conclusions

Atom is a great multipurpose ecosystem with huge extensibility to tackle common tasks, both for general text documents and coding projects. There is support in the form of community developed packages, and complete customisability. By combining a few of these packages, it is possible to build a friendly development/analysis environment for those new to coding, or seasoned veterans alike, which encourages best practices at every turn.

That’s all from me for now – happy coding!