Skip to content
Extraits de code Groupes Projets
Valider c6a26fce rédigé par Frédéric Zinelli's avatar Frédéric Zinelli
Parcourir les fichiers

Revert PyodidePlot related things

parent 1b127ce3
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #47656 réussi
......@@ -30,33 +30,6 @@ title: Images en Python
Votre tracé sera ici
</div>
### Ibis. Utilisation de pyodide_plot, par Frédéric Zinelli
Ceci est une alternative au travail de Nicola Révéret, permettant de se reposer sur différentes macros et bibliothèques personnalisées (voir sur le dépôt: `main.py` et `pyodide_plot`)
!!! question "Tracer le graphe d'une fonction sur une plage donnée"
{{ IDE('pyodide_plot1') }}
{{ figure('cible_1bis') }}
{{ figure('cible_2bis') }}
!!! help "Markdown et codes en jeu"
* le code d'insertion markdown est :
```markdown
{% raw %}{{ IDE('pyodide_plot1') }}
{{ figure('cible_1bis') }}
{{ figure('cible_2bis') }}{% endraw %}
```
* Le code du fichier python est :
{{py('pyodide_plot1')}}
## II. Utiliser la tortue par Romain Janvier
......
# --- PYODIDE:env --- #
import matplotlib # Indispensable
from pyodide_plot import PyodidePlot
pyo_plt_1bis = PyodidePlot('cible_1bis')
pyo_plt_2bis = PyodidePlot('cible_2bis')
# --- PYODIDE:code --- #
# Tracer directement une fonction (spécifique)
pyo_plt_1bis.plot_func(lambda x:x**2, range(1,10), 'r-', title="La fonction carré")
# Les objets PyodidePlot peuvent aussi être utilisés exactement comme matplotlib.plot:
xs = [-2 + k * 0.1 for k in range(41)]
ys = [x**3 for x in xs]
pyo_plt_2bis.plot(xs, ys, 'r-')
pyo_plt_2bis.grid() # Optionnel : pour voir le quadrillage
pyo_plt_2bis.axhline() # Optionnel : pour voir l'axe des abscisses
pyo_plt_2bis.axvline() # Optionnel : pour voir l'axe des ordonnées
pyo_plt_2bis.title("La fonction cube")
pyo_plt_2bis.show()
# -- PYODIDE:ignore --- #
# Nota: on pourrait aussi faire ceci, dans le second cas, pour avoir l'auto-completion:
import matplotlib.pyplot as plt
pyo_plt_2bis.plot(xs, ys, 'r-')
plt.grid() # Optionnel : pour voir le quadrillage
plt.axhline() # Optionnel : pour voir l'axe des abscisses
plt.axvline() # Optionnel : pour voir l'axe des ordonnées
plt.title("La fonction cube")
plt.show()
......@@ -53,14 +53,4 @@
*/
.md-grid {
max-width: 90%;
}
.mat-plt-figure {
display: flex;
justify-content: center;
align-content:center;
flex-direction: column;
margin:auto;
min-height:5em;
text-align:center;
}
\ No newline at end of file
from pyodide_mkdocs_theme.pyodide_macros import (
PyodideMacrosPlugin,
Msg, MsgPlural, TestsToken, Tip,
)
def define_env(env:PyodideMacrosPlugin):
env.lang.overload({
"tests": TestsToken("\n# Tests"),
})
#-------------------------------------
inserted_cdn = set()
CDN = '''<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" integrity="sha512-SnH5WK+bZxgPHs44uWIX+LLJAJ9/2PkPKZ5QiAj6Ta86w+fsb2TkcmfRyVX3pBnMFcV7oQPJkl9QevSCWr3W6A==" crossorigin="anonymous" referrerpolicy="no-referrer" />'''
def cdn_if_not_already_in_page():
page = env.page.file.src_uri
if page in inserted_cdn:
return ""
inserted_cdn.add(page)
return CDN
@env.macro
def figure(id:str, kls:str="", admo='!!!'):
code = f"""
{admo} tip "Votre figure"
<div id="{id}" class="center mat-plt-figure{ kls and ' '+kls }">Votre tracé sera ici</div>
{ cdn_if_not_already_in_page() }
"""
indent = env.get_macro_indent()
if indent:
code = code.replace('\n', '\n'+indent)
return code
......@@ -121,10 +121,7 @@ plugins:
build:
python_libs:
- turtle
- pyodide_plot
tab_to_spaces: 4
macros_with_indents:
- figure
# En remplacement de mkdocs-exclude. Tous les fichiers correspondant aux patterns indiqués seront
......
def __start_module():
import js
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("module://matplotlib_pyodide.html5_canvas_backend")
class PyodidePlot:
def __init__(self, div_id:str):
self.div_id = div_id
def __getattr__(self, prop:str):
return getattr(plt, prop)
def _refresh(self):
js.document.pyodideMplTarget = js.document.getElementById(self.div_id)
js.document.getElementById(self.div_id).textContent = ""
_,ax = plt.subplots()
return ax
def plot_func(self, func, rng, fmt=None, title:str=None):
xs = list(rng)
ys = [*map(func, rng)]
args = (xs,ys) if fmt is None else (xs,ys,fmt)
ax = self._refresh()
ax.plot(*args)
if title: plt.title(title)
plt.show()
def plot(self, *args, **kw):
ax = self._refresh()
return ax.plot(*args, **kw)
return PyodidePlot
PyodidePlot = __start_module()
del __start_module
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter