Skip to main content
Mathematics LibreTexts

43.2: Example running python code in Jupyter Notebooks

  • Page ID
    70560
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    One of the most unique and defining features of Jupyter notebooks is the ability to run code inside of this notebook. This ability makes Jupyter Notebooks especially useful in classes that teach or use programming concepts.

    Jupyter notebooks are separated into different types of “cells”. The two major types of cells are; Markdown cells and code cells. Markdown cells (such as this one) consist of formated text, images and equations much like your favorite word processor.

    The following are two code cells written in the Python programming language. This simple code is a tool to make it easy to search your jupyter notebooks which can be handy if you are looking for something from a previous class. The example searches for an exact string in your notebook files in the current directory and displays links to the files as output.

    To run the code, first select the code cell with your mouse and then hold down the “Shift” key while hitting the “enter” key. You will have to hit the enter key twice to run both cells.

    #Search string
    search_string = "rocket"
    
    #Search current directory
    directory ='.'
    from pathlib import Path
    from IPython.core.display import display, HTML
    
    search_string = search_string.lower()
    links=[]
    
    folder = Path(directory)
    
    files = folder.glob('*.ipynb')
    files = sorted(files)
    for file in files:
        fn = str(file)
        with open(fn,'r', encoding='utf-8') as fp:
            for line in fp:
                line = line.lower()
                if search_string in line:
                    links.append("<a href="+fn+" target=\"_blank\" >"+fn+"</a></br>")
                    break
    if links:
        display(HTML(' '.join(links)))
    else:
        print('string ('+search_string+') not found.')
    string (rocket) not found.
    

    This page titled 43.2: Example running python code in Jupyter Notebooks is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.