{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Workshop \"Teaching Sciences and Engineering with Jupyter Notebooks\" 2022\n", "C. Hardebolle, [CC-BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/)\n", "\n", "
\n", " How to use this notebook?
\n", " This notebook is made of text cells and code cells. The code cells have to be executed to see the result of the program.
To execute a cell, simply select it and click on the \"play\" button () in the tool bar just above the notebook, or type shift + enter.
It is important to execute the code cells in their order of appearance in the notebook.\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting started: narrative and code\n", "\n", "A notebook is made of text cells (\"Markdown\") and code cells.\n", "\n", "## Text cells\n", "\n", "The text you are reading is contained in a text cell, which can be edited by *double clicking* on it.\n", "\n", "You can **format the text** in a notebook using the Markdown syntax. For instance, you can put a sentence in bold by adding double stars (\\*\\*) before and after, **like this.** \n", "The double stars are actually not visible, what you see is the result of the formatting once the text cell has been **rendered**. \n", "To render a text cell, simply select it and click on the \"play\" button () in the tool bar just above the notebook, or type shift + enter.\n", "\n", "Here is an [amazing cheat sheet about the Markdown syntax in Jupyter notebooks](https://medium.com/analytics-vidhya/the-ultimate-markdown-guide-for-jupyter-notebook-d5e5abf728fd) and you can also find the [full reference of the Mardown syntax on John Gruber's website](https://daringfireball.net/projects/markdown/syntax).\n", "\n", "To **add a text cell** to a notebook, click on the + icon in the toolbar at the top, then change the type of the cell to \"Markdown\" using the dropdown menu in the same toolbar.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code cells\n", "\n", "Code cells make it possible to **embed a program** or pieces of program into a notebook, to execute them and **see the results** of its execution right away directly in the notebook.\n", "\n", "The code cell below contains a line of Python code that displays the text \"Hello!\". \n", "To **execute this cell**, simply select it and click on the \"play\" button () in the tool bar just above the notebook, or type shift + enter.
\n", "You should then see the text \"Hello!\" appear just below the code cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Hello!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You might have noticed that a *number between square brackets* has appeared at the left of the code cell after its execution. \n", "This number indicates the **order in which the code cell has been executed**, with respect to the other code cells or to previous executions of the same code cell.\n", "\n", "The order in which different pieces of code are executed affects the execution results, which is why these numbers are important information to consider.\n", "\n", "To **add a code cell** to a notebook, simply click on the + icon in the toolbar at the top.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "---\n", "\n", "# Useful text formatting tools\n", "\n", "Below, we discuss some additional useful tools to format your narrative in notebooks.\n", "\n", "
\n", "\n", "## Spacing / structuring text\n", "\n", "You can space out your text in your notebooks using blank lines. \n", "Unfortunately Markdown has no syntax for blank lines therefore you have to use the HTML syntax `
` each time you want to introduce one.\n", "\n", "To separate paragraphs you can also use horizontal lines, which are easily generated in Markdown with the syntax `---` (triple minus). \n", "The HMTL equivalent is `
`.\n", "\n", "**Example:** Edit the current cell to see how the following blank line is generated, and find the symbols that generate the horizontal line at the top of the section.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Hyperlinks\n", "\n", "To create any type of link in Markdown, the syntax is the following: `[text that will appear](URL)` \n", "If you wish to create a link to a local file in the current folder, for instance another notebook, simply replace the URL with the path of the file, relative to the notebook in which the link is: `[text that will appear](subfolder/file.extension)`\n", "\n", "**Example:** Here is a link to our [Getting Started notebook on interactive visualizations](GettingStarted-02-InteractiveVisualization.ipynb).\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Highlighting important information\n", "\n", "Text cells also support more elaborate [HTML](https://html.com/) and [CSS](https://html.com/css/) formatting, for instance to style paragraphs or titles with colors and backgrounds.\n", "\n", "**Example:** The two cells below illustrate how you can highlight the instructions or solution of an activity. \n", "Feel free to reuse these examples (simply edit the text cell and copy-paste the content)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Activity
\n", "\n", "Add a line to the plot that draws a door to the house. \n", "Then create a slider that will let the user choose the width of the door. \n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Solution** - You check your answer with the solution by clicking on the \"...\" below.\n", " \n", "
\n", "\n", "

" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Hiding cells\n", "\n", "Hidding cells can be used to make a notebook more readable, to hide supplementary content or to hide exercise solutions.\n", "\n", "To hide a cell, click on the blue bar that appears on its left when you select or edit it. \n", "A hidden cell appears as a series of 3 dots `...` on which you can simply click to show the cell again. \n", "If you save your notebook with a hidden cell, the cell will remain hidden when you re-open your notebook (therefore if you share a notebook with hidden cells to students, the cells will be hidden for them).\n", "\n", "\n", "\n", "\n", "\n", "
" ] }, { "attachments": { "a3a2f418-e391-442c-99b9-c118480d23e9.png": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAACwAAAAzCAYAAADsBOpPAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH5gEfDzsECVBQWgAACKtJREFUaN7tmW1sU9cZx//PudfkzQ5QKOJlox0EA+NFAkqZgCkZoH6g+ISEuVvHmjJgYR+mjg0ShzDaWzotTYBCk0qo2ZJqG21XpSN2LKRV0C1dgTLKwqo2g4QS6EAJBQIJdhI79r3PPsShxjiOkxLIhx3Jkn3Oc+753XOe87yZMIxafn7+NwBMB9BQUlJyOZqMGC6weXmO5wG6ANARgC7k5TkKo8nRcIB1OByLmXEsopsBXlxSUnJi2O2wYSAj+maK9MhO9V4vzlqG2tH9xRjDb9ItexpbCeD+Z1FzNDFmbukTODc3d2QgENhERAmqqv6+vLy8pb9l2grmjFbgfwKMJQAWA0jzdjWPBEwgFfA6rOwBWkE4Q8BRnflYalLgfdIu+u4EC1YRKfkAZoZ116ekJL3bpw7/ZP36Y6FFAaLz5uTkWWVlZf5ooLe2TV8C5k3EsANIHOAhtDLwRwiUpxY1nu3tLCgoGM3MWwFMY6bG7m7f7n379rVFBV63bt14EqIlYmB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NAA+zBD7LcUNNfH9yxQDuKKiwgKggIhszMxEdExVVS0nJ+dq1BqYIiQYS9FjP819aRWA4wQ+GtTFX0btbrjwtdN8ACgtLU0wm80fhBYPb5cCgcD8TZs2XY/l+zs7W8axgbEsMAZk6BB8nUhvNf/2wpf3SuHvCJbNZvP6KLAA8E2TybQDwC/6fHOtNgigOfQZshYZS2TEkE0fDlGciMh8/TFku4cdMIDqGLUI57AD3rBhQzURVUSRq/V4PHuGA3BUO1xRUSGJaDUAEzO/f+nSpQOapgWHA/D/AEh/gI+aea01AAAAAElFTkSuQmCC" } }, "cell_type": "markdown", "metadata": {}, "source": [ "## Images\n", "\n", "Images play an important role for understanding explanations, so don't be afraid to embed images (photos, diagrams, sketches, etc.) into your notebooks.\n", "\n", "A first option to include an image in a text cell is to use the Markdown syntax, where you indicate either the path to the image file in your workspace (relative to the notebook you are currently editing) or a URL if you use an online image: `![](relative/path/to/image/file.png)`\n", "\n", "One drawback of the Markdown syntax is that it does not allow you so specify the size of the image, which means your image appears *as is*. \n", "To include an image while modifying the size at which it is displayed, you can use the HTML syntax: ``. If you specify only the width or the height, the ratio of the image will be conserved. If you specify both values then the ratio will be adapted to what you specify.\n", "\n", "If you want to quickly include an image into a notebook (e.g. a screen capture) without going through the steps of saving the image into your workspace then writing the necessary code to embed it, then you can also simply drag-and-drop or copy-paste an image right onto your text cell and Jupyter will take care of everything! (Note: the image is then stored in the metadata of your notebook, therefore you will not be able to retrieve it afterwards, and it increases the size of your notebook...).\n", "\n", "**Examples:** Edit the cell to see the code of the different examples. \n", "![](figs/epfl-logo.png) ![main-logo.png](attachment:a3a2f418-e391-442c-99b9-c118480d23e9.png)\n", "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Math equations\n", "\n", "You can use LaTeX to write mathematical equations in your Markdown cells, both inline like this: $\\lvert\\vec{T}\\rvert$ or in blocks like this:\n", "\n", "$$\n", "\\begin{align}\n", "\\lvert\\vec{T}\\rvert = \\frac{\\frac{1}{2}.m.g}{sin(\\alpha)}\n", "\\end{align}\n", "$$\n", "\n", "Of course you need to be familiar with the LaTeX syntax and symbols. \n", "You can find a quick intro and overview in the [LaTeX chapter](https://personal.math.ubc.ca/~pwalls/math-python/jupyter/latex/) of the online [\"Mathematical Python\"](https://personal.math.ubc.ca/~pwalls/math-python/) textbook (made with notebooks!).\n", "\n", "Note that notebooks support only a subset of the LaTeX mathematical syntax, more specifically it relies on the MathJax subset of LaTeX, more information in the [official documentation of MathJax](https://docs.mathjax.org/en/latest/input/tex/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "# References\n", "\n", "The full documentation of JupyterLab: \n", "https://jupyterlab.readthedocs.io/en/1.2.x/ \n", "\n", "A useful cheat sheet on the Mardown syntax: \n", "https://medium.com/analytics-vidhya/the-ultimate-markdown-guide-for-jupyter-notebook-d5e5abf728fd\n", "\n", "The full reference for the Markdown syntax in Jupyter notebooks: \n", "https://daringfireball.net/projects/markdown/syntax\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }