{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/git/https%3A%2F%2Fgitlab.dsi.universite-paris-saclay.fr%2Fbruno.denis%2Fintro_jupyter/HEAD?labpath=notebooks%2Fpossibilites.ipynb) \n", "[![NbViewer](https://badgen.net/static/render/NbViewer/orange)](https://nbviewer.org/urls/gitlab.dsi.universite-paris-saclay.fr/bruno.denis/intro_jupyter/-/raw/main/notebooks/possibilites.ipynb) \n", "\n", "# Possibilités d'un carnet Jupyter\n", "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "1", "metadata": { "editable": true, "slideshow": { "slide_type": "notes" }, "tags": [] }, "source": [ "Cette cellule contient une note pour le présentateur. Elle sera affichée dnas une secondes fenêtre lorsque ce carnet est utilisé comme diaporama.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "2", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Objectifs de ce carnet\n", "\n", "- Mettre en avant les principales possibilités d'un carnet Jupyter.\n", "- Voir comment chaque aspect d'un carnet est pris en compte en convertissant le carnet.\n" ] }, { "cell_type": "markdown", "id": "3", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Melange texte, formule, code et résultats\n", "\n", "On cherche à déterminer si l'équation $a.x^2 + b.x + c = 0$ admet des solutions réelles pour $x$ sachant que $a, b, c \\in \\mathbb{R}$ sont trois constantes réelles." ] }, { "cell_type": "code", "execution_count": 1, "id": "4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "def nombre_solutions_reelles(a, b, c):\n", " delta = b**2 - 4*a*c\n", " if delta > 0:\n", " return 2 # 2 solutions réelles\n", " elif delta == 0:\n", " return 1 # 1 solution réelle (double)\n", " else:\n", " return 0 # pas de solution réelle" ] }, { "cell_type": "code", "execution_count": 2, "id": "5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nombre_solutions_reelles(2, 1, -1)" ] }, { "cell_type": "markdown", "id": "6", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Tableau et images\n", "\n", "\n", "| | Format SVG | format PNG |\n", "|----------------:|:----------:|:----------:|\n", "| Fichier local | ![SVG local](../assets/img/ampoule.svg)
`../assets/img/ampoule.svg` | ![PNG local](../assets/img/ampoule.png)
`../assets/img/ampoule.png` |\n", "| Fichier distant | ![SVG distant](https://upload.wikimedia.org/wikipedia/commons/e/e7/Dialog-information_on.svg)
`https://upload.wikimedia.org/...` | ![PNG distant](https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Dialog-information_on.svg/48px-Dialog-information_on.svg.png)
`https://upload.wikimedia.org/...` | " ] }, { "cell_type": "markdown", "id": "7", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Lecture d'un fichier CSV en ligne" ] }, { "cell_type": "code", "execution_count": 3, "id": "8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nom_longexploitantxy
0Vaires-TorcySNCF673418.26496.863970e+06
1LognesRATP673078.43186.859970e+06
2TorcyRATP674687.45046.860010e+06
\n", "
" ], "text/plain": [ " nom_long exploitant x y\n", "0 Vaires-Torcy SNCF 673418.2649 6.863970e+06\n", "1 Lognes RATP 673078.4318 6.859970e+06\n", "2 Torcy RATP 674687.4504 6.860010e+06" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas\n", "url = \"https://data.iledefrance-mobilites.fr/api/explore/v2.1/catalog/\"\n", "url += \"datasets/emplacement-des-gares-idf-data-generalisee/exports/csv\"\n", "# Lecture du tableau de données en ligne\n", "df = pandas.read_csv(url, sep=\";\")\n", "# Réduction du tableau à une sélection de 4 colonnes\n", "df = df[[\"nom_long\",\"exploitant\",\"x\",\"y\"]]\n", "df.head(3) # affiche les 3 premières lignes du tableau" ] }, { "cell_type": "markdown", "id": "9", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Interaction avec l'utilisateur\n", "\n", "Le module `ipywidgets` permet la saisie \"graphique\" de donnée et le recalcule automatique d'une fonction Python, ici `affiche_somme_de_2_carres()`" ] }, { "cell_type": "code", "execution_count": 4, "id": "10", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8d95a834ff7e4ae28fe31269888560ab", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=1.0, description='a', max=5.0, min=-5.0), FloatSlider(value=1.0, descr…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ipywidgets\n", "\n", "def affiche_somme_de_2_carres(a=1, b=1):\n", " \"\"\"Calcule et affiche la somme de 2 nombre au carrée.\"\"\"\n", " f_string = \"a² + b² = {:.1f} pour a = {:.1f} et b = {:.1f}\"\n", " print(f_string.format(a**2 + b**2, a, b))\n", "\n", "ipywidgets.interact(\n", " affiche_somme_de_2_carres,\n", " a=(-5.0,5.0,0.1),\n", " b=(-5.0,5.0,0.1),\n", ")" ] }, { "cell_type": "markdown", "id": "11", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "Il est égualement possible d'utiliser un décorateur de fonction `@` pour un code plus compact." ] }, { "cell_type": "code", "execution_count": 5, "id": "12", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bfd8512c969e432f86debe52f055f450", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=1.0, description='a', max=5.0, min=-5.0), FloatSlider(value=1.0, descr…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import ipywidgets\n", "\n", "@ipywidgets.interact(a=(-5.0,5.0,0.1), b=(-5.0,5.0,0.1))\n", "def affiche_somme_de_2_carres(a=1, b=1):\n", " \"\"\"Calcule et affiche la somme de 2 nombre au carrée.\"\"\"\n", " f_string = \"a² + b² = {:.1f} pour a = {:.1f} et b = {:.1f}\"\n", " print(f_string.format(a**2 + b**2, a, b))" ] }, { "cell_type": "markdown", "id": "13", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Interaction avec l'utilisateur (2)\n", "\n", "La fonction trace une elipse dont l'utilisateur choisit de façon interactive le ratio entre son grand axe et son petit axe, et son l'angle par rapport à l'horizontale. \n", "\n", "Importation des modules Python nécessaires" ] }, { "cell_type": "code", "execution_count": 6, "id": "14", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import ipywidgets\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "15", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "Fonction pour générer les points d'une ellipse" ] }, { "cell_type": "code", "execution_count": 7, "id": "16", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "def genere_ellipse(ratio, angle):\n", " \"\"\"Génere des points répartis sur une ellipse.\n", "\n", " Parameters\n", " ----------\n", " ratio : float\n", " Rapport entre le grand axe et le petit axe.\n", " angle : float\n", " Angle entre Ox et le grand axe.\n", "\n", " Returns\n", " -------\n", " x : list\n", " Liste de l'abscisse des points.\n", " y : list\n", " Liste de l'ordonnée des points.\n", " \"\"\"\n", " t = np.linspace(0, 2 * np.pi, 100)\n", " x = ratio * np.cos(t) * np.cos(angle) - 1/ratio * np.sin(t) * np.sin(angle)\n", " y = ratio * np.cos(t) * np.sin(angle) + 1/ratio * np.sin(t) * np.cos(angle)\n", " return x, y" ] }, { "cell_type": "markdown", "id": "17", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "Fonction qui trace des points sur une figure avec la bibliothèque `matplotlib`" ] }, { "cell_type": "code", "execution_count": 8, "id": "18", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "def trace_points(x, y):\n", " \"\"\"Trace les points (x, y).\"\"\"\n", " fig, ax = plt.subplots()\n", " ax.plot(x, y, lw=2)\n", " ax.set_aspect('equal')\n", " ax.set_xlim([-2, 2])\n", " ax.set_ylim([-2, 2])" ] }, { "cell_type": "markdown", "id": "19", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "source": [ "Fonction qui trace des points d'une ellipse" ] }, { "cell_type": "code", "execution_count": 9, "id": "20", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b2f5f54176424c7486d1631890ee11ae", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=1.2000000000000002, description='ratio', max=2.0, min=0.5), FloatSlide…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "@ipywidgets.interact(ratio=(0.5,2.0,0.1), angle=(-3.34/2,3.14/2,0.1))\n", "def trace_ellipse(ratio, angle):\n", " trace_points(*genere_ellipse(ratio, angle))" ] } ], "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.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }