.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/plot_02_overview_skore_ui.py"
.. LINE NUMBERS ARE GIVEN BELOW.
.. only:: html
.. note::
:class: sphx-glr-download-link-note
:ref:`Go to the end `
to download the full example code.
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_examples_plot_02_overview_skore_ui.py:
.. _example_overview_skore_ui:
========================
Overview of the skore UI
========================
This example provides an overview of the functionalities and the different types
of items that you can store in a skore :class:`~skore.Project`.
.. GENERATED FROM PYTHON SOURCE LINES 13-19
Creating and loading a skore project
====================================
We start by creating a temporary directory to store our project such that we can
easily clean it after executing this example. If you want to keep the project,
you have to skip this section.
.. GENERATED FROM PYTHON SOURCE LINES 19-25
.. code-block:: Python
import tempfile
from pathlib import Path
temp_dir = tempfile.TemporaryDirectory(prefix="skore_example_")
temp_dir_path = Path(temp_dir.name)
.. GENERATED FROM PYTHON SOURCE LINES 26-33
.. code-block:: Python
import subprocess
# create the skore project
subprocess.run(
f"python3 -m skore create my_project_ui --working-dir {temp_dir.name}".split()
)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
CompletedProcess(args=['python3', '-m', 'skore', 'create', 'my_project_ui', '--working-dir', '/tmp/skore_example_o926z73z'], returncode=0)
.. GENERATED FROM PYTHON SOURCE LINES 34-39
.. code-block:: Python
from skore import load
my_project_ui = load(temp_dir_path / "my_project_ui.skore")
.. GENERATED FROM PYTHON SOURCE LINES 40-45
Storing integers
================
Now, let us store our first object using :func:`~skore.Project.put`, for example an
integer:
.. GENERATED FROM PYTHON SOURCE LINES 47-49
.. code-block:: Python
my_project_ui.put("my_int", 3)
.. GENERATED FROM PYTHON SOURCE LINES 50-53
Here, the name of the object is ``my_int`` and the integer value is 3.
You can read it from the project by using :func:`~skore.Project.get`:
.. GENERATED FROM PYTHON SOURCE LINES 55-57
.. code-block:: Python
my_project_ui.get("my_int")
.. rst-class:: sphx-glr-script-out
.. code-block:: none
3
.. GENERATED FROM PYTHON SOURCE LINES 58-60
Careful; like in a traditional Python dictionary, the ``put`` method will *overwrite*
past data if you use a key which already exists!
.. GENERATED FROM PYTHON SOURCE LINES 62-64
.. code-block:: Python
my_project_ui.put("my_int", 30_000)
.. GENERATED FROM PYTHON SOURCE LINES 65-66
Let us check the updated value:
.. GENERATED FROM PYTHON SOURCE LINES 68-70
.. code-block:: Python
my_project_ui.get("my_int")
.. rst-class:: sphx-glr-script-out
.. code-block:: none
30000
.. GENERATED FROM PYTHON SOURCE LINES 71-73
By using the :func:`~skore.Project.delete_item` method, you can also delete an object
so that your skore UI does not become cluttered:
.. GENERATED FROM PYTHON SOURCE LINES 75-77
.. code-block:: Python
my_project_ui.put("my_int_2", 10)
.. GENERATED FROM PYTHON SOURCE LINES 78-80
.. code-block:: Python
my_project_ui.delete_item("my_int_2")
.. GENERATED FROM PYTHON SOURCE LINES 81-82
You can display all the keys in your project:
.. GENERATED FROM PYTHON SOURCE LINES 84-86
.. code-block:: Python
my_project_ui.list_item_keys()
.. rst-class:: sphx-glr-script-out
.. code-block:: none
['my_int']
.. GENERATED FROM PYTHON SOURCE LINES 87-89
Storing strings and texts
=========================
.. GENERATED FROM PYTHON SOURCE LINES 91-92
We just stored a integer, now let us store some text using strings!
.. GENERATED FROM PYTHON SOURCE LINES 94-96
.. code-block:: Python
my_project_ui.put("my_string", "Hello world!")
.. GENERATED FROM PYTHON SOURCE LINES 97-99
.. code-block:: Python
my_project_ui.get("my_string")
.. rst-class:: sphx-glr-script-out
.. code-block:: none
'Hello world!'
.. GENERATED FROM PYTHON SOURCE LINES 100-103
:func:`~skore.Project.get` infers the type of the inserted object by default. For
example, strings are assumed to be in Markdown format. Hence, you can customize the
display of your text:
.. GENERATED FROM PYTHON SOURCE LINES 105-118
.. code-block:: Python
my_project_ui.put(
"my_string_2",
(
"""Hello world!, **bold**, *italic*, `code`
```python
def my_func(x):
return x+2
```
"""
),
)
.. GENERATED FROM PYTHON SOURCE LINES 119-121
Moreover, you can also explicitly tell skore the media type of an object, for example
in HTML:
.. GENERATED FROM PYTHON SOURCE LINES 123-132
.. code-block:: Python
from skore.item import MediaItem
my_project_ui.put_item(
"my_string_3",
MediaItem.factory(
"Title
bold, italic, etc.
", media_type="text/html"
),
)
.. GENERATED FROM PYTHON SOURCE LINES 133-135
.. note::
We used :func:`~skore.Project.put_item` instead of :func:`~skore.Project.put`.
.. GENERATED FROM PYTHON SOURCE LINES 137-138
Note that the media type is only used for the UI, and not in this notebook at hand:
.. GENERATED FROM PYTHON SOURCE LINES 140-142
.. code-block:: Python
my_project_ui.get("my_string_3")
.. rst-class:: sphx-glr-script-out
.. code-block:: none
b'Title
bold, italic, etc.'
.. GENERATED FROM PYTHON SOURCE LINES 143-144
You can also conveniently use a Python f-string:
.. GENERATED FROM PYTHON SOURCE LINES 146-152
.. code-block:: Python
x = 2
y = [1, 2, 3, 4]
my_project_ui.put(
"my_string_4", f"The value of `x` is {x} and the value of `y` is {y}."
)
.. GENERATED FROM PYTHON SOURCE LINES 153-155
Storing many kinds of data
==========================
.. GENERATED FROM PYTHON SOURCE LINES 157-158
Python list:
.. GENERATED FROM PYTHON SOURCE LINES 160-164
.. code-block:: Python
my_list = [1, 2, 3, 4]
my_project_ui.put("my_list", my_list)
my_list
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[1, 2, 3, 4]
.. GENERATED FROM PYTHON SOURCE LINES 165-166
Python dictionary:
.. GENERATED FROM PYTHON SOURCE LINES 168-175
.. code-block:: Python
my_dict = {
"company": "probabl",
"year": 2023,
}
my_project_ui.put("my_dict", my_dict)
my_dict
.. rst-class:: sphx-glr-script-out
.. code-block:: none
{'company': 'probabl', 'year': 2023}
.. GENERATED FROM PYTHON SOURCE LINES 176-177
Numpy array:
.. GENERATED FROM PYTHON SOURCE LINES 179-185
.. code-block:: Python
import numpy as np
my_arr = np.random.randn(3, 3)
my_project_ui.put("my_arr", my_arr)
my_arr
.. rst-class:: sphx-glr-script-out
.. code-block:: none
array([[-0.1510416 , -0.09595045, 2.57572983],
[-1.44290927, -0.43199245, -0.90394943],
[ 3.19366996, 0.81142569, -0.65813647]])
.. GENERATED FROM PYTHON SOURCE LINES 186-187
Pandas data frame:
.. GENERATED FROM PYTHON SOURCE LINES 189-195
.. code-block:: Python
import pandas as pd
my_df = pd.DataFrame(np.random.randn(10, 5))
my_project_ui.put("my_df", my_df)
my_df.head()
.. raw:: html
|
0 |
1 |
2 |
3 |
4 |
0 |
-0.553250 |
-1.037511 |
0.344572 |
-0.124828 |
0.118351 |
1 |
0.035065 |
-1.758432 |
-0.703589 |
-1.117504 |
0.390323 |
2 |
0.540673 |
-0.431672 |
-0.448286 |
-0.003490 |
-1.177759 |
3 |
1.108618 |
-0.963264 |
0.338944 |
-1.216301 |
-0.845810 |
4 |
-1.758399 |
1.514302 |
0.983255 |
-0.020762 |
1.604427 |
.. GENERATED FROM PYTHON SOURCE LINES 196-201
Storing data visualizations
===========================
Note that, in the dashboard, the interactivity of plots is supported, for example for
Altair and Plotly.
.. GENERATED FROM PYTHON SOURCE LINES 203-204
Matplotlib figure:
.. GENERATED FROM PYTHON SOURCE LINES 206-222
.. code-block:: Python
import matplotlib.pyplot as plt
x = np.linspace(0, 2, 100)
fig, ax = plt.subplots(layout="constrained", dpi=200)
ax.plot(x, x, label="linear")
ax.plot(x, x**2, label="quadratic")
ax.plot(x, x**3, label="cubic")
ax.set_xlabel("x label")
ax.set_ylabel("y label")
ax.set_title("Simple Plot")
ax.legend()
plt.show()
my_project_ui.put("my_figure", fig)
.. image-sg:: /auto_examples/images/sphx_glr_plot_02_overview_skore_ui_001.png
:alt: Simple Plot
:srcset: /auto_examples/images/sphx_glr_plot_02_overview_skore_ui_001.png
:class: sphx-glr-single-img
.. GENERATED FROM PYTHON SOURCE LINES 223-225
|
Altair chart:
.. GENERATED FROM PYTHON SOURCE LINES 227-244
.. code-block:: Python
import altair as alt
num_points = 100
df_plot = pd.DataFrame(
{"x": np.random.randn(num_points), "y": np.random.randn(num_points)}
)
my_altair_chart = (
alt.Chart(df_plot)
.mark_circle()
.encode(x="x", y="y", tooltip=["x", "y"])
.interactive()
.properties(title="My title")
)
my_project_ui.put("my_altair_chart", my_altair_chart)
.. GENERATED FROM PYTHON SOURCE LINES 245-255
.. note::
For Plotly figures, some users reported the following error when running Plotly
cells: ``ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not
installed``. This is a Plotly issue which is documented `here
`_; to solve it, we recommend
installing nbformat in your environment, e.g. with:
.. code-block:: console
pip install --upgrade nbformat
.. GENERATED FROM PYTHON SOURCE LINES 257-258
Plotly figure:
.. GENERATED FROM PYTHON SOURCE LINES 260-269
.. code-block:: Python
import plotly.express as px
df = px.data.iris()
fig = px.scatter(
df, x=df.sepal_length, y=df.sepal_width, color=df.species, size=df.petal_length
)
my_project_ui.put("my_plotly_fig", fig)
.. GENERATED FROM PYTHON SOURCE LINES 270-271
Animated Plotly figure:
.. GENERATED FROM PYTHON SOURCE LINES 273-291
.. code-block:: Python
df = px.data.gapminder()
my_anim_plotly_fig = px.scatter(
df,
x="gdpPercap",
y="lifeExp",
animation_frame="year",
animation_group="country",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=55,
range_x=[100, 100_000],
range_y=[25, 90],
)
my_project_ui.put("my_anim_plotly_fig", my_anim_plotly_fig)
.. GENERATED FROM PYTHON SOURCE LINES 292-293
PIL image:
.. GENERATED FROM PYTHON SOURCE LINES 295-304
.. code-block:: Python
import io
import PIL
my_pil_image = PIL.Image.new("RGB", (100, 100), color="red")
with io.BytesIO() as output:
my_pil_image.save(output, format="png")
my_project_ui.put("my_pil_image", my_pil_image)
.. GENERATED FROM PYTHON SOURCE LINES 305-312
Storing scikit-learn models and pipelines
=========================================
As skore is developed by `Probabl `_, the spin-off of
scikit-learn, skore treats scikit-learn models and pipelines as first-class citizens.
First of all, you can store a scikit-learn model:
.. GENERATED FROM PYTHON SOURCE LINES 314-320
.. code-block:: Python
from sklearn.linear_model import Lasso
my_model = Lasso(alpha=2)
my_project_ui.put("my_model", my_model)
my_model
.. raw:: html
Lasso(alpha=2)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
.. GENERATED FROM PYTHON SOURCE LINES 321-322
You can also store scikit-learn pipelines:
.. GENERATED FROM PYTHON SOURCE LINES 324-333
.. code-block:: Python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
my_pipeline = Pipeline(
[("standard_scaler", StandardScaler()), ("lasso", Lasso(alpha=2))]
)
my_project_ui.put("my_pipeline", my_pipeline)
my_pipeline
.. raw:: html
Pipeline(steps=[('standard_scaler', StandardScaler()),
('lasso', Lasso(alpha=2))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
.. GENERATED FROM PYTHON SOURCE LINES 334-335
Moreover, you can store fitted scikit-learn pipelines:
.. GENERATED FROM PYTHON SOURCE LINES 337-347
.. code-block:: Python
from sklearn.datasets import load_diabetes
diabetes = load_diabetes()
X = diabetes.data[:150]
y = diabetes.target[:150]
my_pipeline.fit(X, y)
my_project_ui.put("my_fitted_pipeline", my_pipeline)
my_pipeline
.. raw:: html
Pipeline(steps=[('standard_scaler', StandardScaler()),
('lasso', Lasso(alpha=2))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
.. GENERATED FROM PYTHON SOURCE LINES 348-352
Cleanup the project
-------------------
Remove the temporary directory:
.. GENERATED FROM PYTHON SOURCE LINES 352-353
.. code-block:: Python
temp_dir.cleanup()
.. rst-class:: sphx-glr-timing
**Total running time of the script:** (0 minutes 1.520 seconds)
.. _sphx_glr_download_auto_examples_plot_02_overview_skore_ui.py:
.. only:: html
.. container:: sphx-glr-footer sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_02_overview_skore_ui.ipynb `
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_02_overview_skore_ui.py `
.. container:: sphx-glr-download sphx-glr-download-zip
:download:`Download zipped: plot_02_overview_skore_ui.zip `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_