Python Notebook Markdown



Overview

  1. Ipython Notebook Markdown Numbered List
  2. Jupyter Markdown New Line
  3. Python Notebook Markdown Template

Getting started with the Python and R Notebook.

  • Python-Markdown simply outputs standard HTML for tables. As explained in the Markdown syntax rules 'HTML is a publishing format; Markdown is a writing format.' Whether borders are displayed and/or the style of those borders are publishing matters and outside the scope of Markdown. However, basic CSS rules for styling the HTML will work fine.
  • Work With Python Code and Markdown Cells in Jupyter Notebook. Jupyter Notebook file consists of a set of cells that can store text or code.
Markdown

Every Mode report contains an integrated notebook-style environment where analysts can extend their analysis using either Python or R.

The Notebook’s moveable code blocks and markdown cells enable exploratory data analysis, visualization, and collaboration. Notebook environments have a variety of supported Python libraries and R packages installed. You can add the results of output cells to reports, or share a link to the Notebook directly. When Notebook output is included in the Report, that Report’s schedule will re-run the Notebook so all of the data stays in sync.

Using the Notebook

To get started using the Notebook:

  1. Open an existing report or create a new report and run one or more SQL queries from the SQL Editor.
  2. Click New Notebook. Your query results will automatically be loaded into a datasets object.
  3. On the right side panel, click the dropdown to select the environment you want to launch a Notebook from, usually either Python 3.7 or R.

Ipython Notebook Markdown Numbered List

Key elements of the Mode Notebook:

  • Toolbar - Where you can manipulate and run your Notebook, restart the session, export, and more.
  • Cells - Compose code and view results in a Code cell, or contextualize your work with a Markdown cell.
  • Resources Panel - The right side panel provides resources to help you including keyboard shortcuts, external documentation, and supported libraries/packages.
  • Status Indicator - Where you are notified about your Notebook session status.

Toolbar

  1. Run Notebook - Runs all input cells in the Notebook in sequence (from top to bottom)
  2. Restart Session - Stops any current computations running in the Notebook. Restarts the session, thus clearing all the variables, libraries imported, etc. that were defined. However, code in input cells will be available to re-run after the Notebook restarts
  3. Run Cell - Runs code in the selected cell
  4. Add New Cell - Adds new input cell above or below the current cell
  5. Move Cell Up - Moves the current input or markdown cell up
  6. Move Cell Down - Moves the current cell down
  7. Delete Cell - Permanently removes cell from the notebook
  8. Fold Cell - Folds (hides) the current cell. Folded cells can still run
  9. Freeze Cell - Freezes the current input cell so that no changes are allowed; also prevents this cell from running
  10. Markdown/Code dropdown - Allows you to select the type for the current input cell (as code or markdown)
  11. Add output to Report Builder - Adds the output of the selected cell to the Report Builder
  12. Export Notebook - Exports all markdown and input cells as a .py or .r file

Working with cells

There are two types of cells in the Notebook:

Markdown - Markdown cells allow you to add context to your analysis. Markdown cell contains text formatted using Markdown and displays its output in-place when it’s run.

Code - Input Python or R code into the IN section of the cell. When this cell runs, any corresponding output (including visualizations) will be shown in the OUT section.

Notes:

  • When you run your notebook, cells are executed in the order they are displayed, starting from the top cell.
  • To select or change a cell’s type, go to the dropdown menu in the top toolbar and choose Code or Markdown.
  • To run a cell, select it and press Shift + Return. Or click Run Cell in the toolbar.
  • The number next to the cell label will increment by one every time code in the cell is successfully run.
  • To see available methods for an object, type the name of the object followed by . and then press tab.

Notebook Status

The status indicator, located in the bottom right corner of the browser window, will notify you if there is an issue with your session. It may prompt you to restart the kernel.

  • Setting up notebook - Displayed when opening up a new Notebook, or after re-starting your session.
  • Ready - Notebook is ready to go.
  • Running - Your code is executing.
  • Loading dataframes - This message may display for larger datasets while dataframe information is loaded into the Notebook.
  • Notebook has encountered an unexpected error - Your session has crashed and will need to be restarted.
  • There was a problem with your session - Your session has terminated and you need to click Restart to get things working again.
  • Cell is still running. Hang tight! - This can appear when code being run includes long-running, computationally intense functions. The Notebook is still online.
  • Notebook is having trouble, try running again - The Notebook is experiencing problems. Please try running your code again to fix the issue.

Accessing query results

The Notebook has access to the results of every query in your report. However, the way you access those results differs depending on the language you’re using. In each case, all query results are delivered to the Notebook as a custom object called datasets. datasets contains objects of the following type:

Python:pandas DataFrame

R:Data Frame

In your Notebook code, reference query result sets in the datasets list by query name, position, or token. For example:

To return results for:PythonR
First query added to reportdatasets[0]datasets[[1]]
Second query added to reportdatasets[1]datasets[[2]]
Query named ‘Active Users’datasets['Active Users']datasets[['Active Users']]
Query with token ‘6763b688fb54’datasets['6763b688fb54']datasets[['6763b688fb54']]
Numbered

Notes:

  • The datasets object won’t update in the Notebook until after all queries in the report have run successfully.
  • R is 1-indexed and Python is 0-indexed.
  • If you refer to query results by the query name, remember to update your code if you rename the query in your report.
  • The order of the results in the datasets object is based on when the query was added to the report. Renaming a query may change the order it’s displayed in the report editor but will not affect its position in the datasets object.
How to find a query’s token

To find the query token starting from the Notebook or editor, click View in the header, then View details, and then click SQL for the query you wish to use. The URL for SQL contains the query token at the end:

https://app.mode.com/ORGANIZATION_USERNAME/reports/REPORT_TOKEN/queries/QUERY_TOKEN

Output

Adding cell output to your report

Add contents of the OUT section of any Notebook cell to the Report Builder by clicking on the cell and then clicking Add to Report in the toolbar. You can adjust the dimensions and placement of this cell in the Report Builder.

NOTE: Scheduled runs will only re-run the Notebook if the Report view page contains at least one output generated by the Notebook. In this case, the Notebook will re-run as part of the scheduled report run.

Add CSV export to a cell

You can add an export button to a Notebook output cell so viewers can export the calculated results contained in any dataframe to a CSV. The following examples add an export button to an output cell that will generate a downloadable CSV of the query results of a query named “New Users”:

Supported libraries

Mode enables easier access to advanced analytical functions by supporting well-established, public libraries within Mode’s Notebooks. Common use cases include:

  • Data Manipulation - Cleaning, aggregating, and summarizing data.

  • Statistics - Simple things like distributions, regressions, and trend lines, as well as some advanced tasks like predictive modeling and machine learning.

  • Advanced Visualization - Python and R have many visualization libraries, enabling analysts to quickly build charts including heatmaps, small multiples, and distributions.

Python

Mode supports Python version 3.7 in the Notebooks. Each environment comes pre-loaded with the following libraries:

IMPORTANT: We strongly discourage using either the requests or pygsheets libraries to access APIs that require authentication using personally identifiable credentials and information, as they will be visible to viewers of your report.

Edge

Mode provides access to an additional Python 3 environment called Python 3 Edge where pending library upgrades are staged. Analysts should use Edge as an alternative environment where they can test out the updated versions of supported Python libraries without fear of jeopardizing scheduled reports.

Mode will announce periodic scheduled promotion events via emails to Mode account administrators. Users will have at least 30 days from that time for testing and validation before the library updates will be made in the broader Python 3 environment. Any Notebooks using the Edge environment will be migrated to use the Python 3 environment at the same time.

Edge currently contains all of the same libraries and versions as the Python 3.7 environment. The next set of updates will be posted shortly.

Analysts can access Edge via the environment drop down in the upper right hand corner of the Notebook. When switching between environments, remember to Restart the Notebook session.

Legacy Python 2 Environment

The Python 2.7 environment is currently only accessible for Mode organizations created before October 2018. Mode no longer supports the Python 2 environment.

Mode, along with the majority of the Python community, ended support for Python 2 at the beginning of 2020 when the open source Python community. Users are encouraged to update necessary reports to use Python 3.7.

Learn more about the advantages of using Python 3 over Python 2 as well as some background about why Python 3 exists. Python 2.7 code will likely not execute in a Python 3.7 environment without some modification. There are a number of syntax differences to take into consideration when migrating to Python 3.7. For more information, read about porting Python 2 code to Python 3.

Many libraries that Mode provided in Python 2 have updated versions available in the Python 3 environment:

R

The Notebook supports R version 3.6.1 and comes pre-loaded with the following R packages:

IMPORTANT: We strongly discourage using the httr library to access APIs that require authentication using personally identifiable credentials and information, as they will be visible to viewers of your report.

Install additional libraries

To use a publicly available library in the Notebook that is not listed above, users leverage each environment’s package manager to install that library at run-time. The Notebook environment has up to 1 GB of memory available to load additional packages.

IMPORTANT: This offers a workaround to try to install additional libraries, beyond what Mode currently supports, into the Notebook. Only supported libraries have been tested to function as expected in Mode’s Notebooks.

Mode’s Notebook architecture does not enable manually installed libraries to have access to the Notebook’s kernel. This means that manually installed versions of popular and interactive libraries like Plotly, Bokeh, and ipywidgets will not function as expected even if the package install appears to succeed.

Unlike officially supported libraries, you must install packages for any additional libraries in each individual report’s Notebook environment. You must add the below package installation commands to the Notebook in each report where you want the corresponding libraries to be available. Avoiding these commands can result in the library not installing and or importing properly.

WARNING: Some libraries require authentication with credentials (e.g., Tweepy, requests, etc.). We strongly discourage using libraries that require authentication using personally identifiable credentials and information, as these credentials will be visible to viewers of your report.

Python

First, enter the following command into a Notebook cell for each public package that you want to install into the Python Notebook, as demonstrated below with the bloom-filter package (replace bloom-filter with the name of the package you want to install):

Users must specify the temporary space as the installation location. Omission of this argument will cause the command to fail.

Alternatively, users can try to upgrade a supported package to a more recent version using:

Next, in a subsequent cell, add an import statement for each library that you want to include in your environment. For example:

You may now use any of the methods or functionality included in the library in subsequent Notebook cells.

R

First, enter the following command into a Notebook cell for each public package that you want to install into the R Notebook, as demonstrated below with the random package (replace random with the name of the package you want to install):

Next, invoke the library command for each library you want to include in your environment from the installed package(s). For example:

You may now use any of the methods or functionality included in the library in subsequent Notebook cells.

FAQs

How much memory is available to the Notebook?

Each Notebook session has the following resources available, depending on the version of your Mode organization:

Available memoryRun-time limitSuspend after idle for
Mode Studio4 GB60 minutes30 minutes
Mode Paid Plans16 GB12 hours60 minutes

When suspended, the Notebook environment can be resumed at any time by running a cell, running the entire Notebook, or running the report.

Last updated February 11, 2021

Thanks for your feedback!

Jupyter notebook tutorial on how to install, run, and use Jupyter for interactive matplotlib plotting, data analysis, and publishing code


New to Plotly?

Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Introduction¶

Jupyter has a beautiful notebook that lets you write and execute code, analyze data, embed content, and share reproducible work. Jupyter Notebook (previously referred to as IPython Notebook) allows you to easily share your code, data, plots, and explanation in a sinle notebook. Publishing is flexible: PDF, HTML, ipynb, dashboards, slides, and more. Code cells are based on an input and output format. For example:

Jupyter Markdown New Line

Installation¶

There are a few ways to use a Jupyter Notebook:

  • Install with pip. Open a terminal and type: $ pip install jupyter.
  • Windows users can install with setuptools.
  • Anaconda and Enthought allow you to download a desktop version of Jupyter Notebook.
  • nteract allows users to work in a notebook enviornment via a desktop application.
  • Microsoft Azure provides hosted access to Jupyter Notebooks.
  • Domino Data Lab offers web-based Notebooks.
  • tmpnb launches a temporary online Notebook for individual users.

Getting Started¶

Python Notebook Markdown

Once you've installed the Notebook, you start from your terminal by calling $ jupyter notebook. This will open a browser on a localhost to the URL of your Notebooks, by default http://127.0.0.1:8888. Windows users need to open up their Command Prompt. You'll see a dashboard with all your Notebooks. You can launch your Notebooks from there. The Notebook has the advantage of looking the same when you're coding and publishing. You just have all the options to move code, run cells, change kernels, and use Markdown when you're running a NB.

Helpful Commands¶

- Tab Completion: Jupyter supports tab completion! You can type object_name.<TAB> to view an object’s attributes. For tips on cell magics, running Notebooks, and exploring objects, check out the Jupyter docs.
- Help: provides an introduction and overview of features.

- Quick Reference: open quick reference by running:

- Keyboard Shortcuts:Shift-Enter will run a cell, Ctrl-Enter will run a cell in-place, Alt-Enter will run a cell and insert another below. See more shortcuts here.

Languages¶

The bulk of this tutorial discusses executing python code in Jupyter notebooks. You can also use Jupyter notebooks to execute R code. Skip down to the [R section] for more information on using IRkernel with Jupyter notebooks and graphing examples.

Package Management¶

When installing packages in Jupyter, you either need to install the package in your actual shell, or run the ! prefix, e.g.:

You may want to reload submodules if you've edited the code in one. IPython comes with automatic reloading magic. You can reload all changed modules before executing a new line.

Some useful packages that we'll use in this tutorial include:

  • Pandas: import data via a url and create a dataframe to easily handle data for analysis and graphing. See examples of using Pandas here: https://plotly.com/pandas/.
  • NumPy: a package for scientific computing with tools for algebra, random number generation, integrating with databases, and managing data. See examples of using NumPy here: https://plotly.com/numpy/.
  • SciPy: a Python-based ecosystem of packages for math, science, and engineering.
  • Plotly: a graphing library for making interactive, publication-quality graphs. See examples of statistic, scientific, 3D charts, and more here: https://plotly.com/python.

Import Data¶

You can use pandas read_csv() function to import data. In the example below, we import a csv hosted on github and display it in a table using Plotly:

Use dataframe.column_title to index the dataframe:

Most pandas functions also work on an entire dataframe. For example, calling std() calculates the standard deviation for each column.

Plotting Inline¶

You can use Plotly's python API to plot inside your Jupyter Notebook by calling plotly.plotly.iplot() or plotly.offline.iplot() if working offline. Plotting in the notebook gives you the advantage of keeping your data analysis and plots in one place. Now we can do a bit of interactive plotting. Head to the Plotly getting started page to learn how to set your credentials. Calling the plot with iplot automaticallly generates an interactive version of the plot inside the Notebook in an iframe. See below:

Plotting multiple traces and styling the chart with custom colors and titles is simple with Plotly syntax. Additionally, you can control the privacy with sharing set to public, private, or secret.

Now we have interactive charts displayed in our notebook. Hover on the chart to see the values for each bar, click and drag to zoom into a specific section or click on the legend to hide/show a trace.

Plotting Interactive Maps¶

Plotly is now integrated with Mapbox. In this example we'll plot lattitude and longitude data of nuclear waste sites. To plot on Mapbox maps with Plotly you'll need a Mapbox account and a Mapbox Access Token which you can add to your Plotly settings.

3D Plotting¶

Using Numpy and Plotly, we can make interactive 3D plots in the Notebook as well.

Animated Plots¶

Checkout Plotly's animation documentation to see how to create animated plots inline in Jupyter notebooks like the Gapminder plot displayed below:

Plot Controls & IPython widgets¶

Add sliders, buttons, and dropdowns to your inline chart:

Additionally, IPython widgets allow you to add sliders, widgets, search boxes, and more to your Notebook. See the widget docs for more information. For others to be able to access your work, they'll need IPython. Or, you can use a cloud-based NB option so others can run your work.

Executing R Code¶

IRkernel, an R kernel for Jupyter, allows you to write and execute R code in a Jupyter notebook. Checkout the IRkernel documentation for some simple installation instructions. Once IRkernel is installed, open a Jupyter Notebook by calling $ jupyter notebook and use the New dropdown to select an R notebook.

See a full R example Jupyter Notebook here: https://plotly.com/~chelsea_lyn/14069

Additional Embed Features¶

We've seen how to embed Plotly tables and charts as iframes in the notebook, with IPython.display we can embed additional features, such a videos. For example, from YouTube:

LaTeX¶

We can embed LaTeX inside a Notebook by putting a $$ around our math, then run the cell as a Markdown cell. For example, the cell below is $$c = sqrt{a^2 + b^2}$$, but the Notebook renders the expression.

Or, you can display output from Python, as seen here.

$displaystyle F(k) = int_{-infty}^{infty} f(x) e^{2pi i k} dx$

Exporting & Publishing Notebooks¶

We can export the Notebook as an HTML, PDF, .py, .ipynb, Markdown, and reST file. You can also turn your NB into a slideshow. You can publish Jupyter Notebooks on Plotly. Simply visit plot.ly and select the + Create button in the upper right hand corner. Select Notebook and upload your Jupyter notebook (.ipynb) file!The notebooks that you upload will be stored in your Plotly organize folder and hosted at a unique link to make sharing quick and easy.See some example notebooks:

Notebook

Publishing Dashboards¶

Users publishing interactive graphs can also use Plotly's dashboarding tool to arrange plots with a drag and drop interface. These dashboards can be published, embedded, and shared.

Publishing Dash Apps¶

For users looking to ship and productionize Python apps, dash is an assemblage of Flask, Socketio, Jinja, Plotly and boiler plate CSS and JS for easily creating data visualization web-apps with your Python data analysis backend.

Jupyter Gallery¶

Python Notebook Markdown Template

For more Jupyter tutorials, checkout Plotly's python documentation: all documentation is written in jupyter notebooks that you can download and run yourself or checkout these user submitted examples!