maartenbreddels commited on
Commit
b20b499
1 Parent(s): 0449aa7

upgrade to use solara 1.29

Browse files

We can now use the pylab interface, so we now use almost the original
notebook.

Polished the UI a bit and added support for dark mode.

pages/00-readme.py CHANGED
@@ -17,4 +17,6 @@ def Page():
17
 
18
  @solara.component
19
  def Layout(children):
20
- solara.AppLayout(children=children)
 
 
 
17
 
18
  @solara.component
19
  def Layout(children):
20
+ dark_effective = solara.lab.use_dark_effective()
21
+ # I like a dark toolbar always, just a different color in dark mode
22
+ return solara.AppLayout(children=children, toolbar_dark=True, color=None if dark_effective else "primary")
pages/01-original-with-widgets.ipynb CHANGED
@@ -82,11 +82,9 @@
82
  " alpha=0.5,\n",
83
  " );\n",
84
  " disp.ax_.scatter(X[:, 0], X[:, 1], c=y, edgecolor=\"k\");\n",
85
- " # plt.title(f\"{classifier.__class__.__name__}\");\n",
86
  " disp.ax_.set_title(f\"{classifier.__class__.__name__}\");\n",
87
- " # plt.show();\n",
88
- " import solara\n",
89
- " solara.display(solara.FigureMatplotlib(fig))\n",
90
  "widget.observe(on_change, names=[\"data\"])\n",
91
  "on_change(None)\n",
92
  "page = ipywidgets.HBox([widget, output])\n",
 
82
  " alpha=0.5,\n",
83
  " );\n",
84
  " disp.ax_.scatter(X[:, 0], X[:, 1], c=y, edgecolor=\"k\");\n",
85
+ " plt.title(f\"{classifier.__class__.__name__}\");\n",
86
  " disp.ax_.set_title(f\"{classifier.__class__.__name__}\");\n",
87
+ " plt.show();\n",
 
 
88
  "widget.observe(on_change, names=[\"data\"])\n",
89
  "on_change(None)\n",
90
  "page = ipywidgets.HBox([widget, output])\n",
pages/02-fancy-with-solara.py CHANGED
@@ -19,7 +19,9 @@ import pandas as pd
19
  from drawdata import ScatterWidget
20
 
21
  drawdata: solara.Reactive[List[Dict]] = solara.reactive([])
22
-
 
 
23
 
24
  @solara.component
25
  def ClassifierDraw(classifier, X, y, response_method="predict_proba", figsize=(8, 8)):
@@ -83,12 +85,13 @@ def Page():
83
  # TODO: doesn't work, ScatterWidget does not update when data is updated (read only?)
84
  # solara.Button(icon_name="mdi-delete", on_click=lambda: drawdata.set([]), icon=True)
85
  # demo how solara can dynamically change the layout
 
86
  solara.Button(icon_name="mdi-align-vertical-top" if vertical.value else "mdi-align-horizontal-left", on_click=lambda: vertical.set(not vertical.value), icon=True)
87
 
88
- # if solara.lab.theme.dark_effective:
89
- # plt.style.use('dark_background')
90
- # else:
91
- # plt.style.use('default')
92
  with solara.Column() if vertical.value else solara.Row():
93
  # with solara, we don't just create the widget, but an element that describes it
94
  # and instead of observe, we have on_<trait> callbacks
@@ -97,23 +100,25 @@ def Page():
97
  ScatterWidget.element(data=drawdata.value, on_data=drawdata.set)
98
  # downside of using elements and components: we cannot call method on the widget
99
  # so we need to re-create the dataframe ourselves
100
- with solara.lab.Tabs():
101
  with solara.lab.Tab("classifier"):
102
- if df is not None and (df["color"].nunique() > 1):
103
- with solara.Column(style={"max-height": "500px", "padding-top": "0px"}):
104
- with solara.lab.Tabs():
105
- with solara.lab.Tab("DecisionTreeClassifier"):
106
- DecisionTreeClassifierDraw(df)
107
- with solara.lab.Tab("LogisticRegressionDraw"):
108
- LogisticRegressionDraw(df)
109
- else:
110
- with solara.Column(style={"justify-content": "center"}) if not vertical.value else solara.Row():
111
- solara.Info("Choose at least two colors to draw a decision boundary.")
 
112
  with solara.lab.Tab("table view"):
113
- if df is not None:
114
- with solara.FileDownload(data=lambda: df.to_csv(), filename="drawdata.csv"):
115
- solara.Button("download as csv", icon_name="mdi-download", outlined=True, color="primary")
116
- solara.DataFrame(df)
 
117
 
118
 
119
  # in the notebook:
 
19
  from drawdata import ScatterWidget
20
 
21
  drawdata: solara.Reactive[List[Dict]] = solara.reactive([])
22
+ # we keep the active tab in a reactive var so the state does not get lost when we change
23
+ # the orientation of the page (vertical or horizontal)
24
+ tab = solara.reactive(0)
25
 
26
  @solara.component
27
  def ClassifierDraw(classifier, X, y, response_method="predict_proba", figsize=(8, 8)):
 
85
  # TODO: doesn't work, ScatterWidget does not update when data is updated (read only?)
86
  # solara.Button(icon_name="mdi-delete", on_click=lambda: drawdata.set([]), icon=True)
87
  # demo how solara can dynamically change the layout
88
+ solara.lab.ThemeToggle(enable_auto=False)
89
  solara.Button(icon_name="mdi-align-vertical-top" if vertical.value else "mdi-align-horizontal-left", on_click=lambda: vertical.set(not vertical.value), icon=True)
90
 
91
+ dark_background = solara.lab.use_dark_effective()
92
+ plt.style.use('dark_background' if dark_background else 'default')
93
+
94
+
95
  with solara.Column() if vertical.value else solara.Row():
96
  # with solara, we don't just create the widget, but an element that describes it
97
  # and instead of observe, we have on_<trait> callbacks
 
100
  ScatterWidget.element(data=drawdata.value, on_data=drawdata.set)
101
  # downside of using elements and components: we cannot call method on the widget
102
  # so we need to re-create the dataframe ourselves
103
+ with solara.lab.Tabs(value=tab):
104
  with solara.lab.Tab("classifier"):
105
+ with solara.Column(classes=["py-4"]): # some nice y padding
106
+ if df is not None and (df["color"].nunique() > 1):
107
+ with solara.Column(style={"max-height": "500px", "padding-top": "0px"}):
108
+ with solara.lab.Tabs():
109
+ with solara.lab.Tab("DecisionTreeClassifier"):
110
+ DecisionTreeClassifierDraw(df)
111
+ with solara.lab.Tab("LogisticRegressionDraw"):
112
+ LogisticRegressionDraw(df)
113
+ else:
114
+ with solara.Column(style={"justify-content": "center"}) if not vertical.value else solara.Row():
115
+ solara.Info("Choose at least two colors to draw a decision boundary.")
116
  with solara.lab.Tab("table view"):
117
+ with solara.Column(classes=["py-4"]): # some nice y padding
118
+ if df is not None:
119
+ with solara.FileDownload(data=lambda: df.to_csv(), filename="drawdata.csv"):
120
+ solara.Button("download as csv", icon_name="mdi-download", outlined=True, color="primary")
121
+ solara.DataFrame(df)
122
 
123
 
124
  # in the notebook:
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- solara==1.28
2
  drawdata==0.3.0
3
  matplotlib
4
  scikit-learn
 
1
+ solara==1.29
2
  drawdata==0.3.0
3
  matplotlib
4
  scikit-learn