Posts by Tags

analysis

blogging

Adding copy buttons to code blocks in Sphinx

3 minute read

Published:

NOTE: This is now a sphinx extension! Thanks to some friendly suggestions, I’ve written this up as a super tiny sphinx extension. Check it out here: https://github.com/choldgraf/sphinx-copybutton

Sphinx is a fantastic way to build documentation for your Python package. On the Jupyter project, we use it for almost all of our repositories.

A common use for Sphinx is to step people through a chunk of code. For example, in the Zero to JupyterHub for Kubernetes guide we step users through a number of installation and configuration steps.

A common annoyance is that there is a lot of copy/pasting involved. Sometimes you accidentally miss a character or some whitespace. So, I spent a bit of time figuring out how to automatically embed a copy button into code blocks. It turns out this is pretty easy!

Here’s what the final result will look like (just hover the code block below)

wow = this_text
is_so = much*more*copyable

Adding a copy button to your Sphinx code blocks

To accomplish this we’ll use the excellent clipboard.js which provides the machinery for copying the contents of an HTML element as well as jquery for modifying our built documentation on-demand.

The result will be a Sphinx site with code blocks that display a copy button when you hover over them. You can see how it looks on this very page, which uses a similar method (but is built with Jekyll).

Here’s what you should do:

  1. Add the clipboard.js javascript. Create a javascript script called doc/_static/custom.js. In the file, put the following code (see comments for explanation):

     function addCopyButtonToCode(){
     // get all code elements
     var allCodeBlocksElements = $( "div.highlight pre" );
    
     // For each element, do the following steps
     allCodeBlocksElements.each(function(ii) {
     // define a unique id for this element and add it
     var currentId = "codeblock" + (ii + 1);
     $(this).attr('id', currentId);
    
     // create a button that's configured for clipboard.js
     // point it to the text that's in this code block
     // add the button just after the text in the code block w/ jquery
     var clipButton = '<button class="btn copybtn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
        $(this).after(clipButton);
     });
    
     // tell clipboard.js to look for clicks that match this query
     new Clipboard('.btn');
     }
    
     $(document).ready(function () {
     // Once the DOM is loaded for the page, attach clipboard buttons
     addCopyButtonToCode();
     });
    
  2. Add some CSS to make it pretty. Create a custom CSS file called doc/_static/custom.css (or add to one you’ve already got). In the file, put these lines:

    /* Copy buttons */
    button.copybtn {
      webkit-transition: opacity .3s ease-in-out;
      -o-transition: opacity .3s ease-in-out;
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 4px;
    }
    div.highlight:hover .copybtn, div.highlight .copybtn:focus {
        opacity: .3;
    }
    div.highlight .copybtn:hover {
        opacity: 1;
    }
    div.highlight {
        position: relative;
    }
    
  3. Link these scripts in your configuration. You need to link your custom JS and CSS scripts, as well as the clipboard.js script so it ships with your site. In your conf.py file, add the following function/lines (or add to one you’ve already got defined).

    def setup(app):
        app.add_stylesheet('custom.css')
        app.add_javascript("custom.js")
        app.add_javascript("https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js")
    

And that’s it! Once you clear your Sphinx cache and re-build your site, you should now have buttons that appear when you hover over them, and that copy the text inside when you click them.

Thanks

Many thanks to this StackOverflow post for the majority of the code that led to this hack!

Blogging with Jupyter Notebooks and Jekyll using nbconvert templates

3 minute read

Published:

Here’s a quick (and hopefully helpful) post for those wishing to blog in Jekyll using Jupyter notebooks. As some of you may know, nbconvert can easily convert your .ipynb files to markdown, which Jekyll can easily turn into blog posts for you.

brains

colormaps

Introducing makeitpop, a tool to perceptually warp your data!

15 minute read

Published:

Feature Image

Note: It should go without saying, but you should never do the stuff that you’re about to read about here. Data is meant to speak for itself, and our visualizations should accurately reflect the data above all else.

communities

Summer conference report back

6 minute read

Published:

This is a short update on several of the conferences and workshops over the summer of this year. There’s all kinds of exciting things going on in open source and open communities, so this is a quick way for me to collect my thoughts on some things I’ve learned this summer.

community

I like Rust’s governance structure

9 minute read

Published:

Recently I’ve been reading up on governance models for several large-ish open source projects. This is partially because I’m involved in a bunch of these projects myself, and partially because it’s fascinating to see distributed groups of people organizing themselves in effective (or not) ways on the internet.

computation

conferences

Summer conference report back

6 minute read

Published:

This is a short update on several of the conferences and workshops over the summer of this year. There’s all kinds of exciting things going on in open source and open communities, so this is a quick way for me to collect my thoughts on some things I’ve learned this summer.

5 things I learned at SciPy

4 minute read

Published:

I’ve finally decompressed after my first go-around with Scipy. For those who haven’t heard of this conference before, Scipy is an annual meeting where members of scientific community get together to discuss their love of Python, scientific programming, and open science. It spans both academics and people from industry, making it a unique place in terms of how software interfaces with scientific research. (if you’re interested the full set of Scipy conferences, check out here.

correlation

data

Craigslist data analysis

10 minute read

Published:

Feature Image

Using Craigslist to compare prices in the Bay Area

In the last post I showed how to use a simple python bot to scrape data from Criagslist. This is a quick follow-up to take a peek at the data.

dev-ops

An academic scientist goes to DevOps Days

8 minute read

Published:

Last week I took a few days to attend DevOpsDays Silicon Valley. My goal was to learn a bit about how the DevOps culture works, what are the things people are excited about and discuss in this community. I’m also interested in learning a thing or two that could be brought back into the scientific / academic world. Here are a couple of thoughts from the experience.

documentation

Using CircleCI to preview documentation in Pull Requests

4 minute read

Published:

Writing documentation is important - it’s the first point of contact between many users and your project, and can be a pivotal moment in whether they decide to adopt your tech or become a contributor.

Adding copy buttons to code blocks in Sphinx

3 minute read

Published:

NOTE: This is now a sphinx extension! Thanks to some friendly suggestions, I’ve written this up as a super tiny sphinx extension. Check it out here: https://github.com/choldgraf/sphinx-copybutton

Sphinx is a fantastic way to build documentation for your Python package. On the Jupyter project, we use it for almost all of our repositories.

A common use for Sphinx is to step people through a chunk of code. For example, in the Zero to JupyterHub for Kubernetes guide we step users through a number of installation and configuration steps.

A common annoyance is that there is a lot of copy/pasting involved. Sometimes you accidentally miss a character or some whitespace. So, I spent a bit of time figuring out how to automatically embed a copy button into code blocks. It turns out this is pretty easy!

Here’s what the final result will look like (just hover the code block below)

wow = this_text
is_so = much*more*copyable

Adding a copy button to your Sphinx code blocks

To accomplish this we’ll use the excellent clipboard.js which provides the machinery for copying the contents of an HTML element as well as jquery for modifying our built documentation on-demand.

The result will be a Sphinx site with code blocks that display a copy button when you hover over them. You can see how it looks on this very page, which uses a similar method (but is built with Jekyll).

Here’s what you should do:

  1. Add the clipboard.js javascript. Create a javascript script called doc/_static/custom.js. In the file, put the following code (see comments for explanation):

     function addCopyButtonToCode(){
     // get all code elements
     var allCodeBlocksElements = $( "div.highlight pre" );
    
     // For each element, do the following steps
     allCodeBlocksElements.each(function(ii) {
     // define a unique id for this element and add it
     var currentId = "codeblock" + (ii + 1);
     $(this).attr('id', currentId);
    
     // create a button that's configured for clipboard.js
     // point it to the text that's in this code block
     // add the button just after the text in the code block w/ jquery
     var clipButton = '<button class="btn copybtn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
        $(this).after(clipButton);
     });
    
     // tell clipboard.js to look for clicks that match this query
     new Clipboard('.btn');
     }
    
     $(document).ready(function () {
     // Once the DOM is loaded for the page, attach clipboard buttons
     addCopyButtonToCode();
     });
    
  2. Add some CSS to make it pretty. Create a custom CSS file called doc/_static/custom.css (or add to one you’ve already got). In the file, put these lines:

    /* Copy buttons */
    button.copybtn {
      webkit-transition: opacity .3s ease-in-out;
      -o-transition: opacity .3s ease-in-out;
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 4px;
    }
    div.highlight:hover .copybtn, div.highlight .copybtn:focus {
        opacity: .3;
    }
    div.highlight .copybtn:hover {
        opacity: 1;
    }
    div.highlight {
        position: relative;
    }
    
  3. Link these scripts in your configuration. You need to link your custom JS and CSS scripts, as well as the clipboard.js script so it ships with your site. In your conf.py file, add the following function/lines (or add to one you’ve already got defined).

    def setup(app):
        app.add_stylesheet('custom.css')
        app.add_javascript("custom.js")
        app.add_javascript("https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js")
    

And that’s it! Once you clear your Sphinx cache and re-build your site, you should now have buttons that appear when you hover over them, and that copy the text inside when you click them.

Thanks

Many thanks to this StackOverflow post for the majority of the code that led to this hack!

efficiency

fft

Could Brexit have happened by chance?

11 minute read

Published:

Feature Image

As a scientist, watching the Brexit vote was a little bit painful. Though probably not for the reason you’re thinking. No, it wasn’t the politics that bothered me, but the method for making such an incredibly important decision. Let me explain…

The beauty of computational efficiency

11 minute read

Published:

Feature Image

When we discuss “computational efficiency”, you often hear people throw around phrases like $O(n^2)$ or $O(nlogn)$. We talk about them in the abstract, and it can be hard to appreciate what these distinctions mean and how important they are. So let’s take a quick look at what computational efficiency looks like in the context of a very famous algorithm: The Fourier Transform.

funnel

governance

grants

NIH grant analysis

9 minute read

Published:

Feature Image

NIH Fellowship Success Rates

As I’m entering the final years of graduate school, I’ve been applying for a few typical “pre-doc” fellowships. One of these is the NRSA, which is notorious for requiring you to wade through forests of beaurocratic documents (seriously, their “guidelines” for writing an NRSA are over 100 pages!). Doing so ends up taking a LOT of time.

holidays

Brainy Jingle Bells

3 minute read

Published:

Feature Image

This is a quick demo of how I created this video. Check it out below, or read on to see the code that made it!

jekyll

Blogging with Jupyter Notebooks and Jekyll using nbconvert templates

3 minute read

Published:

Here’s a quick (and hopefully helpful) post for those wishing to blog in Jekyll using Jupyter notebooks. As some of you may know, nbconvert can easily convert your .ipynb files to markdown, which Jekyll can easily turn into blog posts for you.

jupyter

Summer conference report back

6 minute read

Published:

This is a short update on several of the conferences and workshops over the summer of this year. There’s all kinds of exciting things going on in open source and open communities, so this is a quick way for me to collect my thoughts on some things I’ve learned this summer.

open

Dates in python

8 minute read

Published:

Feature Image

As a part of setting up the website for the Docathon I’ve had to re-learn all of my date string formatting rules. It’s one of those little problems you don’t really think about - turning an arbitrary string into something structured like a date - until you’ve actually got to do it.

Matplotlib Cyclers are Great

1 minute read

Published:

Feature Image

Every now and then I come across a nifty feature in Matplotlib that I wish I’d known about earlier. The MPL documentation can be a beast to get through, and as a result you miss some cool stuff sometimes.

Brainy Jingle Bells

3 minute read

Published:

Feature Image

This is a quick demo of how I created this video. Check it out below, or read on to see the code that made it!

5 things I learned at SciPy

4 minute read

Published:

I’ve finally decompressed after my first go-around with Scipy. For those who haven’t heard of this conference before, Scipy is an annual meeting where members of scientific community get together to discuss their love of Python, scientific programming, and open science. It spans both academics and people from industry, making it a unique place in terms of how software interfaces with scientific research. (if you’re interested the full set of Scipy conferences, check out here.

open-science

An academic scientist goes to DevOps Days

8 minute read

Published:

Last week I took a few days to attend DevOpsDays Silicon Valley. My goal was to learn a bit about how the DevOps culture works, what are the things people are excited about and discuss in this community. I’m also interested in learning a thing or two that could be brought back into the scientific / academic world. Here are a couple of thoughts from the experience.

plots

preprints

programming

Scraping craigslist

14 minute read

Published:

Feature Image

Overview

In this notebook, I’ll show you how to make a simple query on Craigslist using some nifty python modules. You can take advantage of all the structure data that exists on webpages to collect interesting datasets.

python

Summer conference report back

6 minute read

Published:

This is a short update on several of the conferences and workshops over the summer of this year. There’s all kinds of exciting things going on in open source and open communities, so this is a quick way for me to collect my thoughts on some things I’ve learned this summer.

Adding copy buttons to code blocks in Sphinx

3 minute read

Published:

NOTE: This is now a sphinx extension! Thanks to some friendly suggestions, I’ve written this up as a super tiny sphinx extension. Check it out here: https://github.com/choldgraf/sphinx-copybutton

Sphinx is a fantastic way to build documentation for your Python package. On the Jupyter project, we use it for almost all of our repositories.

A common use for Sphinx is to step people through a chunk of code. For example, in the Zero to JupyterHub for Kubernetes guide we step users through a number of installation and configuration steps.

A common annoyance is that there is a lot of copy/pasting involved. Sometimes you accidentally miss a character or some whitespace. So, I spent a bit of time figuring out how to automatically embed a copy button into code blocks. It turns out this is pretty easy!

Here’s what the final result will look like (just hover the code block below)

wow = this_text
is_so = much*more*copyable

Adding a copy button to your Sphinx code blocks

To accomplish this we’ll use the excellent clipboard.js which provides the machinery for copying the contents of an HTML element as well as jquery for modifying our built documentation on-demand.

The result will be a Sphinx site with code blocks that display a copy button when you hover over them. You can see how it looks on this very page, which uses a similar method (but is built with Jekyll).

Here’s what you should do:

  1. Add the clipboard.js javascript. Create a javascript script called doc/_static/custom.js. In the file, put the following code (see comments for explanation):

     function addCopyButtonToCode(){
     // get all code elements
     var allCodeBlocksElements = $( "div.highlight pre" );
    
     // For each element, do the following steps
     allCodeBlocksElements.each(function(ii) {
     // define a unique id for this element and add it
     var currentId = "codeblock" + (ii + 1);
     $(this).attr('id', currentId);
    
     // create a button that's configured for clipboard.js
     // point it to the text that's in this code block
     // add the button just after the text in the code block w/ jquery
     var clipButton = '<button class="btn copybtn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
        $(this).after(clipButton);
     });
    
     // tell clipboard.js to look for clicks that match this query
     new Clipboard('.btn');
     }
    
     $(document).ready(function () {
     // Once the DOM is loaded for the page, attach clipboard buttons
     addCopyButtonToCode();
     });
    
  2. Add some CSS to make it pretty. Create a custom CSS file called doc/_static/custom.css (or add to one you’ve already got). In the file, put these lines:

    /* Copy buttons */
    button.copybtn {
      webkit-transition: opacity .3s ease-in-out;
      -o-transition: opacity .3s ease-in-out;
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 4px;
    }
    div.highlight:hover .copybtn, div.highlight .copybtn:focus {
        opacity: .3;
    }
    div.highlight .copybtn:hover {
        opacity: 1;
    }
    div.highlight {
        position: relative;
    }
    
  3. Link these scripts in your configuration. You need to link your custom JS and CSS scripts, as well as the clipboard.js script so it ships with your site. In your conf.py file, add the following function/lines (or add to one you’ve already got defined).

    def setup(app):
        app.add_stylesheet('custom.css')
        app.add_javascript("custom.js")
        app.add_javascript("https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js")
    

And that’s it! Once you clear your Sphinx cache and re-build your site, you should now have buttons that appear when you hover over them, and that copy the text inside when you click them.

Thanks

Many thanks to this StackOverflow post for the majority of the code that led to this hack!

Introducing makeitpop, a tool to perceptually warp your data!

15 minute read

Published:

Feature Image

Note: It should go without saying, but you should never do the stuff that you’re about to read about here. Data is meant to speak for itself, and our visualizations should accurately reflect the data above all else.

Blogging with Jupyter Notebooks and Jekyll using nbconvert templates

3 minute read

Published:

Here’s a quick (and hopefully helpful) post for those wishing to blog in Jekyll using Jupyter notebooks. As some of you may know, nbconvert can easily convert your .ipynb files to markdown, which Jekyll can easily turn into blog posts for you.

An academic scientist goes to DevOps Days

8 minute read

Published:

Last week I took a few days to attend DevOpsDays Silicon Valley. My goal was to learn a bit about how the DevOps culture works, what are the things people are excited about and discuss in this community. I’m also interested in learning a thing or two that could be brought back into the scientific / academic world. Here are a couple of thoughts from the experience.

science

Dates in python

8 minute read

Published:

Feature Image

As a part of setting up the website for the Docathon I’ve had to re-learn all of my date string formatting rules. It’s one of those little problems you don’t really think about - turning an arbitrary string into something structured like a date - until you’ve actually got to do it.

scraping

simulations

sphinx

Using CircleCI to preview documentation in Pull Requests

4 minute read

Published:

Writing documentation is important - it’s the first point of contact between many users and your project, and can be a pivotal moment in whether they decide to adopt your tech or become a contributor.

Adding copy buttons to code blocks in Sphinx

3 minute read

Published:

NOTE: This is now a sphinx extension! Thanks to some friendly suggestions, I’ve written this up as a super tiny sphinx extension. Check it out here: https://github.com/choldgraf/sphinx-copybutton

Sphinx is a fantastic way to build documentation for your Python package. On the Jupyter project, we use it for almost all of our repositories.

A common use for Sphinx is to step people through a chunk of code. For example, in the Zero to JupyterHub for Kubernetes guide we step users through a number of installation and configuration steps.

A common annoyance is that there is a lot of copy/pasting involved. Sometimes you accidentally miss a character or some whitespace. So, I spent a bit of time figuring out how to automatically embed a copy button into code blocks. It turns out this is pretty easy!

Here’s what the final result will look like (just hover the code block below)

wow = this_text
is_so = much*more*copyable

Adding a copy button to your Sphinx code blocks

To accomplish this we’ll use the excellent clipboard.js which provides the machinery for copying the contents of an HTML element as well as jquery for modifying our built documentation on-demand.

The result will be a Sphinx site with code blocks that display a copy button when you hover over them. You can see how it looks on this very page, which uses a similar method (but is built with Jekyll).

Here’s what you should do:

  1. Add the clipboard.js javascript. Create a javascript script called doc/_static/custom.js. In the file, put the following code (see comments for explanation):

     function addCopyButtonToCode(){
     // get all code elements
     var allCodeBlocksElements = $( "div.highlight pre" );
    
     // For each element, do the following steps
     allCodeBlocksElements.each(function(ii) {
     // define a unique id for this element and add it
     var currentId = "codeblock" + (ii + 1);
     $(this).attr('id', currentId);
    
     // create a button that's configured for clipboard.js
     // point it to the text that's in this code block
     // add the button just after the text in the code block w/ jquery
     var clipButton = '<button class="btn copybtn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
        $(this).after(clipButton);
     });
    
     // tell clipboard.js to look for clicks that match this query
     new Clipboard('.btn');
     }
    
     $(document).ready(function () {
     // Once the DOM is loaded for the page, attach clipboard buttons
     addCopyButtonToCode();
     });
    
  2. Add some CSS to make it pretty. Create a custom CSS file called doc/_static/custom.css (or add to one you’ve already got). In the file, put these lines:

    /* Copy buttons */
    button.copybtn {
      webkit-transition: opacity .3s ease-in-out;
      -o-transition: opacity .3s ease-in-out;
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 4px;
    }
    div.highlight:hover .copybtn, div.highlight .copybtn:focus {
        opacity: .3;
    }
    div.highlight .copybtn:hover {
        opacity: 1;
    }
    div.highlight {
        position: relative;
    }
    
  3. Link these scripts in your configuration. You need to link your custom JS and CSS scripts, as well as the clipboard.js script so it ships with your site. In your conf.py file, add the following function/lines (or add to one you’ve already got defined).

    def setup(app):
        app.add_stylesheet('custom.css')
        app.add_javascript("custom.js")
        app.add_javascript("https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js")
    

And that’s it! Once you clear your Sphinx cache and re-build your site, you should now have buttons that appear when you hover over them, and that copy the text inside when you click them.

Thanks

Many thanks to this StackOverflow post for the majority of the code that led to this hack!

statistics

timeseries

visualization

Craigslist data analysis

10 minute read

Published:

Feature Image

Using Craigslist to compare prices in the Bay Area

In the last post I showed how to use a simple python bot to scrape data from Criagslist. This is a quick follow-up to take a peek at the data.

visualizations

Matplotlib Cyclers are Great

1 minute read

Published:

Feature Image

Every now and then I come across a nifty feature in Matplotlib that I wish I’d known about earlier. The MPL documentation can be a beast to get through, and as a result you miss some cool stuff sometimes.

voting

web