Skip to content

Commit

Permalink
configure jupyter lite. and update the analysis code, so it works wit…
Browse files Browse the repository at this point in the history
…h pyodide kernel too.
  • Loading branch information
bitnik committed Sep 25, 2024
1 parent 789b452 commit eb385e2
Show file tree
Hide file tree
Showing 11 changed files with 17,902 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
python -m pip install -r requirements.txt
- name: Build the JupyterLite site
run: |
jupyter lite build --contents notebooks --output-dir dist
jupyter lite build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

LOCAL.md

# jupyterlite
*.doit.db
_output
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
# - id: requirements-txt-fixer
- id: check-json
- id: pretty-format-json
files: ^.*.json$
args:
- --autofix
- --no-sort-keys
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ Or run JupyterLite server:

```sh
# https://jupyterlite.readthedocs.io/en/stable/quickstart/standalone.html
jupyter lite build --contents notebooks
jupyter lite serve
jupyter lite build
jupyter lite serve --port 8889
```
25 changes: 11 additions & 14 deletions jupyter-lite.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"description": "The jupyter-lite.json file is used to configure the runtime configuration of JupyterLite.",
"jupyter-lite-schema-version": 0,
"jupyter-config-data": {
"appName": "analyse-caesar-cipher",
"disabledExtensions": [
"@jupyterlab/drawio-extension",
"jupyterlab-kernel-spy",
"jupyterlab-tour"
],
"settingsStorageName": "jupyterlite-analyse-caesar-cipher-storage",
"LiteBuildConfig": {
"ignore_sys_prefix": ["federated_extensions"]
}
}
"description": "The jupyter-lite.json file is used to configure the runtime configuration of JupyterLite.",
"jupyter-lite-schema-version": 0,
"jupyter-config-data": {
"appName": "analyse-caesar-cipher",
"disabledExtensions": [
"@jupyterlab/drawio-extension",
"jupyterlab-kernel-spy",
"jupyterlab-tour"
],
"settingsStorageName": "jupyterlite-analyse-caesar-cipher-storage"
}
}
9 changes: 9 additions & 0 deletions jupyter_lite_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"description": "This file is used to configure the build time configuration of JupyterLite.",
"LiteBuildConfig": {
"contents": [
"notebooks"
],
"output_dir": "dist"
}
}
94 changes: 56 additions & 38 deletions notebooks/analyse_caesar_cipher.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,20 @@
"metadata": {},
"outputs": [],
"source": [
"from urllib.request import urlopen\n",
"\n",
"# from collections import Counter\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# def calculate_letter_frequency(text: str) -> dict:\n",
"# letter_freq: dict = {}\n",
"# text = text.lower()\n",
"# total_count = 0\n",
"# for char in text:\n",
"# if char in ascii_lowercase and char not in letter_freq:\n",
"# letter_freq[char] = text.count(char)\n",
"# return letter_freq\n",
"\n",
"\n",
"def calculate_letter_frequency(text: str) -> dict:\n",
" letter_freq: dict = {}\n",
" text = text.lower()\n",
" total_count = 0\n",
" for char in text:\n",
" if char in ascii_lowercase:\n",
" if char in ALPHABET:\n",
" total_count += 1\n",
" if char not in letter_freq:\n",
" letter_freq[char] = text.count(char)\n",
" letter_freq[char] = 1\n",
" else:\n",
" letter_freq[char] += 1\n",
" for l, f in letter_freq.items():\n",
" letter_freq[l] = f * 100 / total_count\n",
" return letter_freq\n",
Expand All @@ -173,22 +163,27 @@
" plt.xlabel(\"Characters\")\n",
" plt.ylabel(\"Frequency %\")\n",
" plt.title(title)\n",
" plt.show()\n",
"\n",
"\n",
"url_hamlet = \"https://www.gutenberg.org/cache/epub/1787/pg1787.txt\"\n",
"with urlopen(url_hamlet) as f:\n",
" text = f.read().decode(\"utf-8\").strip()\n",
" # print(Counter(text))\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2b74dcd",
"metadata": {},
"outputs": [],
"source": [
"# Exmaples with example data\n",
"with open(\"./data/hamlet.txt\") as f:\n",
" text = f.read().strip()\n",
" letter_freq = calculate_letter_frequency(text)\n",
" print(letter_freq)\n",
" # print(letter_freq)\n",
" plot_histogram(letter_freq)\n",
"\n",
"url_romeo_and_juliet = \"https://www.gutenberg.org/cache/epub/47960/pg47960.txt\"\n",
"with urlopen(url_romeo_and_juliet) as f:\n",
" text = f.read().decode(\"utf-8\").strip()\n",
"with open(\"./data/romeo_and_juliet.txt\") as f:\n",
" text = f.read().strip()\n",
" letter_freq = calculate_letter_frequency(text)\n",
" print(letter_freq)\n",
" # print(letter_freq)\n",
" plot_histogram(letter_freq)"
]
},
Expand Down Expand Up @@ -284,24 +279,45 @@
{
"cell_type": "code",
"execution_count": null,
"id": "058099ed-d266-4ce1-80b1-17371492bedd",
"id": "ed08ebd7",
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import HTML, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e8ee124",
"metadata": {},
"outputs": [],
"source": [
"# Ex: Brvruja rmnjb jan dbnm rw vxmnaw mjcj-lxvyanbbrxw cnlqwrzdnb bdlq jb Qdoovjw lxmrwp.\n",
"# https://cryptii.com/pipes/caesar-cipher\n",
"print(\"\")\n",
"example_encrypted_text = input(\"Give me a cipher text to break:\")\n",
"print(\"\")\n",
"\n",
"display(HTML(f\"<strong>Give me a cipher text to break</strong>: \"))\n",
"example_encrypted_text = input(\"\")\n",
"\n",
"# Workaround for Pyodide kernel, its `input` function returns PyodideFuture object.\n",
"if type(example_encrypted_text) != str:\n",
" example_encrypted_text = await example_encrypted_text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "058099ed-d266-4ce1-80b1-17371492bedd",
"metadata": {},
"outputs": [],
"source": [
"letter_freq = calculate_letter_frequency(example_encrypted_text)\n",
"plot_histogram(letter_freq, title=\"Frequency analysis of given encrypted text\")\n",
"plot_histogram(LETTER_FREQUENCY, title=\"Frequency analysis of English alphabet\")\n",
"\n",
"encryption_key = break_cipher(example_encrypted_text)\n",
"print(\"\")\n",
"decrypted_text = decrypt(example_encrypted_text, encryption_key)\n",
"print(f\"Decrypted text: {decrypted_text}\")"
"display(HTML(f\"<br><strong>Decrypted text</strong>: {decrypted_text}\"))"
]
},
{
Expand All @@ -320,19 +336,20 @@
"outputs": [],
"source": [
"# example = \"My name is John.\"\n",
"# print(example)\n",
"# print(f\"Plain text: {example}\")\n",
"# letter_freq = calculate_letter_frequency(example)\n",
"# plot_histogram(letter_freq)\n",
"\n",
"# encrypted_text = encrypt(example, 9)\n",
"# print(encrypted_text)\n",
"# print(f\"Encrypted text: {encrypted_text}\")\n",
"# letter_freq = calculate_letter_frequency(encrypted_text)\n",
"# plot_histogram(letter_freq)\n",
"\n",
"# encryption_key = break_cipher(encrypted_text)\n",
"# print(encryption_key)\n",
"# print(f\"Encryption key: {encryption_key}\")\n",
"\n",
"# decrypt(encrypted_text, encryption_key)"
"# decrypted_text = decrypt(encrypted_text, encryption_key)\n",
"# display(HTML(f\"<br><strong>Decrypted text</strong>: {decrypted_text}\"))"
]
},
{
Expand All @@ -346,8 +363,9 @@
"# # If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out.\n",
"# example = \"Vs ur unq nalguvat pbasvqragvny gb fnl, ur jebgr vg va pvcure, gung vf, ol fb punatvat gur beqre bs gur yrggref bs gur nycunorg, gung abg n jbeq pbhyq or znqr bhg.\"\n",
"# encryption_key = break_cipher(example)\n",
"# print(encryption_key)\n",
"# decrypt(example, encryption_key)"
"# print(f\"Encryption key: {encryption_key}\")\n",
"# decrypted_text = decrypt(example, encryption_key)\n",
"# display(HTML(f\"<br><strong>Decrypted text</strong>: {decrypted_text}\"))"
]
},
{
Expand Down
4 changes: 4 additions & 0 deletions notebooks/data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```sh
wget https://www.gutenberg.org/cache/epub/1787/pg1787.txt -O hamlet.txt
wget https://www.gutenberg.org/cache/epub/47960/pg47960.txt -O romeo_and_juliet.txt
```
Loading

0 comments on commit eb385e2

Please sign in to comment.