Skip to content

✨ add static info #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions markdown_generator/mdconverter_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from nbconvert import MarkdownExporter
from nbconvert.preprocessors import Preprocessor
import nbformat
import matplotlib.pyplot as plt


class Ndconverter:
Expand Down Expand Up @@ -220,6 +221,13 @@ class MultiNdconverter(CustomMdconverter):
def __init__(self, filenames: list) -> None:
super().__init__()
self.filenames = filenames
self.static = self._init_static_dict(
keys=["python_counts", "markdown_counts", "code_counts", "total_counts"]
)

def _init_static_dict(self, keys: list[str]) -> dict:
"""Initialize a dictionary with given keys and empty dictionaries as values."""
return {key: {} for key in keys}

def add_file(self, filename: str) -> None:
"""Add file to convert"""
Expand All @@ -230,3 +238,57 @@ def run(self, save_on: bool = True) -> None:
for filename in self.filenames:
self.filename = filename
super().run(save_on)

def cal_static(self) -> None:
for filename in self.filenames:
self.filename = filename
super()._load_ipynb() # self.notebook_content

python_version = self.notebook_content["metadata"]["language_info"][
"version"
]
markdown_count = sum(
1
for cell in self.notebook_content["cells"]
if cell["cell_type"] == "markdown"
)
code_count = sum(
1
for cell in self.notebook_content["cells"]
if cell["cell_type"] == "code"
)
total_count = markdown_count + code_count

self._increment_count(python_version, self.static["python_counts"])
self._increment_count(markdown_count, self.static["markdown_counts"])
self._increment_count(code_count, self.static["code_counts"])
self._increment_count(total_count, self.static["total_counts"])

@staticmethod
def _increment_count(key: str, count_dict: dict) -> None:
"""Increment count for a key in the given dictionary."""
count_dict[str(key)] = count_dict.get(str(key), 0) + 1

def plot_static_data(self, category: str) -> None:
if category not in self.static:
print(f"Category '{category}' not found in data.")
return

counts = self.static[category]
keys = list(counts.keys())
values = list(map(int, counts.values()))

# Plot the data
plt.figure(figsize=(10, 6))
plt.bar(keys, values)

# Customize the chart
plt.title(f"Counts for {category}")
plt.xlabel("Keys")
plt.ylabel("Counts")
plt.xticks(rotation=45, ha="right")
plt.grid(axis="y", linestyle="--", alpha=0.7)

# Show the chart
plt.tight_layout()
plt.show()
Loading