5
5
from nbconvert import MarkdownExporter
6
6
from nbconvert .preprocessors import Preprocessor
7
7
import nbformat
8
+ import matplotlib .pyplot as plt
8
9
9
10
10
11
class Ndconverter :
@@ -220,6 +221,13 @@ class MultiNdconverter(CustomMdconverter):
220
221
def __init__ (self , filenames : list ) -> None :
221
222
super ().__init__ ()
222
223
self .filenames = filenames
224
+ self .static = self ._init_static_dict (
225
+ keys = ["python_counts" , "markdown_counts" , "code_counts" , "total_counts" ]
226
+ )
227
+
228
+ def _init_static_dict (self , keys : list [str ]) -> dict :
229
+ """Initialize a dictionary with given keys and empty dictionaries as values."""
230
+ return {key : {} for key in keys }
223
231
224
232
def add_file (self , filename : str ) -> None :
225
233
"""Add file to convert"""
@@ -230,3 +238,57 @@ def run(self, save_on: bool = True) -> None:
230
238
for filename in self .filenames :
231
239
self .filename = filename
232
240
super ().run (save_on )
241
+
242
+ def cal_static (self ) -> None :
243
+ for filename in self .filenames :
244
+ self .filename = filename
245
+ super ()._load_ipynb () # self.notebook_content
246
+
247
+ python_version = self .notebook_content ["metadata" ]["language_info" ][
248
+ "version"
249
+ ]
250
+ markdown_count = sum (
251
+ 1
252
+ for cell in self .notebook_content ["cells" ]
253
+ if cell ["cell_type" ] == "markdown"
254
+ )
255
+ code_count = sum (
256
+ 1
257
+ for cell in self .notebook_content ["cells" ]
258
+ if cell ["cell_type" ] == "code"
259
+ )
260
+ total_count = markdown_count + code_count
261
+
262
+ self ._increment_count (python_version , self .static ["python_counts" ])
263
+ self ._increment_count (markdown_count , self .static ["markdown_counts" ])
264
+ self ._increment_count (code_count , self .static ["code_counts" ])
265
+ self ._increment_count (total_count , self .static ["total_counts" ])
266
+
267
+ @staticmethod
268
+ def _increment_count (key : str , count_dict : dict ) -> None :
269
+ """Increment count for a key in the given dictionary."""
270
+ count_dict [str (key )] = count_dict .get (str (key ), 0 ) + 1
271
+
272
+ def plot_static_data (self , category : str ) -> None :
273
+ if category not in self .static :
274
+ print (f"Category '{ category } ' not found in data." )
275
+ return
276
+
277
+ counts = self .static [category ]
278
+ keys = list (counts .keys ())
279
+ values = list (map (int , counts .values ()))
280
+
281
+ # Plot the data
282
+ plt .figure (figsize = (10 , 6 ))
283
+ plt .bar (keys , values )
284
+
285
+ # Customize the chart
286
+ plt .title (f"Counts for { category } " )
287
+ plt .xlabel ("Keys" )
288
+ plt .ylabel ("Counts" )
289
+ plt .xticks (rotation = 45 , ha = "right" )
290
+ plt .grid (axis = "y" , linestyle = "--" , alpha = 0.7 )
291
+
292
+ # Show the chart
293
+ plt .tight_layout ()
294
+ plt .show ()
0 commit comments