dp_wizard_templates 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dp_wizard_templates might be problematic. Click here for more details.
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/.coveragerc +1 -1
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/.flake8 +4 -3
- dp_wizard_templates-0.2.0/.nojekyll +1 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/.pre-commit-config.yaml +2 -1
- dp_wizard_templates-0.2.0/CHANGELOG.md +10 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/PKG-INFO +4 -8
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/README.md +1 -6
- dp_wizard_templates-0.2.0/dp_wizard_templates/VERSION +1 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/dp_wizard_templates/code_template.py +7 -3
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/dp_wizard_templates/converters.py +21 -8
- {dp_wizard_templates-0.1.0/README_examples → dp_wizard_templates-0.2.0/examples}/hello-world.html +4 -4
- {dp_wizard_templates-0.1.0/README_examples → dp_wizard_templates-0.2.0/examples}/hello-world.ipynb +7 -5
- dp_wizard_templates-0.2.0/index.html +7767 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/pyproject.toml +3 -2
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/tests/test_converters.py +1 -1
- dp_wizard_templates-0.2.0/tests/test_index_html.py +204 -0
- dp_wizard_templates-0.1.0/CHANGELOG.md +0 -5
- dp_wizard_templates-0.1.0/README_test.py +0 -139
- dp_wizard_templates-0.1.0/dp_wizard_templates/VERSION +0 -1
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/.github/workflows/test.yml +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/.gitignore +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/.pytest.ini +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/LICENSE +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/dp_wizard_templates/__init__.py +0 -0
- {dp_wizard_templates-0.1.0/README_examples → dp_wizard_templates-0.2.0/examples}/_block_demo.py +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/requirements-dev.in +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/requirements-dev.txt +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/requirements.in +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/requirements.txt +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/scripts/changelog.py +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/scripts/ci.sh +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/scripts/requirements.py +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/tests/fixtures/fake-executed.ipynb +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/tests/fixtures/fake.ipynb +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/tests/fixtures/fake.py +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/tests/test_code_template.py +0 -0
- {dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/tests/test_misc.py +0 -0
|
@@ -9,6 +9,7 @@ extend-ignore = E203,E501,E701
|
|
|
9
9
|
|
|
10
10
|
per-file-ignores =
|
|
11
11
|
# Ignore undefined names in templates.
|
|
12
|
-
|
|
13
|
-
# Ignore mid-file imports: These
|
|
14
|
-
|
|
12
|
+
examples/*.py:F821,F401,E302
|
|
13
|
+
# Ignore mid-file imports: These make for a better exposition.
|
|
14
|
+
# Ignore lines that too long. Mostly URLs.
|
|
15
|
+
test_index_html.py:E402,B950
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Use index.html instead of Jekyll.
|
|
@@ -3,7 +3,8 @@ repos:
|
|
|
3
3
|
rev: v2.3.0
|
|
4
4
|
hooks:
|
|
5
5
|
- id: check-yaml
|
|
6
|
-
|
|
6
|
+
# This conflicts with generated notebooks:
|
|
7
|
+
# - id: end-of-file-fixer
|
|
7
8
|
- id: trailing-whitespace
|
|
8
9
|
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
|
|
9
10
|
- repo: https://github.com/psf/black-pre-commit-mirror
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dp_wizard_templates
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Code templating tools
|
|
5
5
|
Author-email: The OpenDP Project <info@opendp.org>
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
@@ -10,7 +10,8 @@ Requires-Dist: ipykernel
|
|
|
10
10
|
Requires-Dist: jupyter-client
|
|
11
11
|
Requires-Dist: jupytext
|
|
12
12
|
Requires-Dist: nbconvert
|
|
13
|
-
Project-URL:
|
|
13
|
+
Project-URL: GitHub, https://github.com/opendp/dp-wizard-templates
|
|
14
|
+
Project-URL: Homepage, https://opendp.github.io/dp-wizard-templates
|
|
14
15
|
|
|
15
16
|
# DP Wizard Templates
|
|
16
17
|
|
|
@@ -19,12 +20,7 @@ Project-URL: Home, https://github.com/opendp/dp-wizard-templates
|
|
|
19
20
|
DP Wizard Templates lets you use syntactically valid Python code as a template.
|
|
20
21
|
Templates can be filled and composed to generate entire notebooks.
|
|
21
22
|
|
|
22
|
-
[
|
|
23
|
-
and an example [output notebook](https://github.com/opendp/dp-wizard-templates/blob/main/README_examples/hello-world.ipynb)
|
|
24
|
-
is also available.
|
|
25
|
-
|
|
26
|
-
DP Wizard Templates was developed for [DP Wizard](https://github.com/opendp/dp-wizard),
|
|
27
|
-
and that codebase remains a good place to look for further examples.
|
|
23
|
+
See the [documentation](https://opendp.github.io/dp-wizard-templates) for more information.
|
|
28
24
|
|
|
29
25
|
|
|
30
26
|
## Development
|
|
@@ -5,12 +5,7 @@
|
|
|
5
5
|
DP Wizard Templates lets you use syntactically valid Python code as a template.
|
|
6
6
|
Templates can be filled and composed to generate entire notebooks.
|
|
7
7
|
|
|
8
|
-
[
|
|
9
|
-
and an example [output notebook](https://github.com/opendp/dp-wizard-templates/blob/main/README_examples/hello-world.ipynb)
|
|
10
|
-
is also available.
|
|
11
|
-
|
|
12
|
-
DP Wizard Templates was developed for [DP Wizard](https://github.com/opendp/dp-wizard),
|
|
13
|
-
and that codebase remains a good place to look for further examples.
|
|
8
|
+
See the [documentation](https://opendp.github.io/dp-wizard-templates) for more information.
|
|
14
9
|
|
|
15
10
|
|
|
16
11
|
## Development
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.2.0
|
{dp_wizard_templates-0.1.0 → dp_wizard_templates-0.2.0}/dp_wizard_templates/code_template.py
RENAMED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import inspect
|
|
1
2
|
import re
|
|
3
|
+
import black
|
|
2
4
|
|
|
3
5
|
|
|
4
6
|
def _get_body(func):
|
|
5
|
-
import inspect
|
|
6
|
-
import re
|
|
7
7
|
|
|
8
8
|
source_lines = inspect.getsource(func).splitlines()
|
|
9
9
|
first_line = source_lines[0]
|
|
@@ -116,7 +116,7 @@ class Template:
|
|
|
116
116
|
raise Exception(base_message)
|
|
117
117
|
return self
|
|
118
118
|
|
|
119
|
-
def finish(self) -> str:
|
|
119
|
+
def finish(self, reformat=False) -> str:
|
|
120
120
|
unfilled_slots = self._initial_slots & self._find_slots()
|
|
121
121
|
if unfilled_slots:
|
|
122
122
|
slots_str = ", ".join(sorted(f"'{slot}'" for slot in unfilled_slots))
|
|
@@ -124,4 +124,8 @@ class Template:
|
|
|
124
124
|
f"{slots_str} slot not filled "
|
|
125
125
|
f"in {self._source}:\n\n{self._template}"
|
|
126
126
|
)
|
|
127
|
+
|
|
128
|
+
if reformat:
|
|
129
|
+
self._template = black.format_str(self._template, mode=black.Mode())
|
|
130
|
+
|
|
127
131
|
return self._template
|
|
@@ -7,6 +7,7 @@ import json
|
|
|
7
7
|
import nbformat
|
|
8
8
|
import nbconvert
|
|
9
9
|
import jupytext
|
|
10
|
+
import black
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
def _is_kernel_installed() -> bool:
|
|
@@ -27,7 +28,9 @@ class ConversionException(Exception):
|
|
|
27
28
|
return f"Script to notebook conversion failed: {self.command}\n{self.stderr})"
|
|
28
29
|
|
|
29
30
|
|
|
30
|
-
def convert_py_to_nb(
|
|
31
|
+
def convert_py_to_nb(
|
|
32
|
+
python_str: str, title: str, execute: bool = False, reformat: bool = True
|
|
33
|
+
):
|
|
31
34
|
"""
|
|
32
35
|
Given Python code as a string, returns a notebook as a string.
|
|
33
36
|
Calls jupytext as a subprocess:
|
|
@@ -43,6 +46,9 @@ def convert_py_to_nb(python_str: str, title: str, execute: bool = False):
|
|
|
43
46
|
|
|
44
47
|
temp_dir_path = Path(temp_dir)
|
|
45
48
|
py_path = temp_dir_path / "input.py"
|
|
49
|
+
if reformat:
|
|
50
|
+
# Line length determined by PDF rendering.
|
|
51
|
+
python_str = black.format_str(python_str, mode=black.Mode(line_length=74))
|
|
46
52
|
py_path.write_text(python_str)
|
|
47
53
|
|
|
48
54
|
argv = [executable] + "-m jupytext --from .py --to .ipynb --output -".split(" ")
|
|
@@ -82,7 +88,9 @@ def _clean_nb(nb_json: str):
|
|
|
82
88
|
for cell in nb["cells"]:
|
|
83
89
|
if "pip install" in cell["source"][0]:
|
|
84
90
|
cell["outputs"] = []
|
|
85
|
-
|
|
91
|
+
# "Coda" may, or may not be followed by "\n".
|
|
92
|
+
# Be flexible!
|
|
93
|
+
if any(line.startswith("# Coda") for line in cell["source"]):
|
|
86
94
|
break
|
|
87
95
|
# Make ID stable:
|
|
88
96
|
cell["id"] = _stable_hash(cell["source"])
|
|
@@ -96,13 +104,9 @@ def _clean_nb(nb_json: str):
|
|
|
96
104
|
return json.dumps(nb, indent=1)
|
|
97
105
|
|
|
98
106
|
|
|
99
|
-
def convert_nb_to_html(python_nb: str):
|
|
100
|
-
return _convert_nb(python_nb, nbconvert.HTMLExporter)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
def _convert_nb(python_nb: str, exporter_constructor):
|
|
107
|
+
def convert_nb_to_html(python_nb: str, numbered=True):
|
|
104
108
|
notebook = nbformat.reads(python_nb, as_version=4)
|
|
105
|
-
exporter =
|
|
109
|
+
exporter = nbconvert.HTMLExporter(
|
|
106
110
|
template_name="lab",
|
|
107
111
|
# The "classic" template's CSS forces large code cells on to
|
|
108
112
|
# the next page rather than breaking, so use "lab" instead.
|
|
@@ -116,4 +120,13 @@ def _convert_nb(python_nb: str, exporter_constructor):
|
|
|
116
120
|
# ],
|
|
117
121
|
)
|
|
118
122
|
(body, _resources) = exporter.from_notebook_node(notebook)
|
|
123
|
+
if not numbered:
|
|
124
|
+
body = body.replace(
|
|
125
|
+
"</head>",
|
|
126
|
+
"""
|
|
127
|
+
<style>
|
|
128
|
+
.jp-InputPrompt {display: none;}
|
|
129
|
+
</style>
|
|
130
|
+
</head>""",
|
|
131
|
+
)
|
|
119
132
|
return body
|
{dp_wizard_templates-0.1.0/README_examples → dp_wizard_templates-0.2.0/examples}/hello-world.html
RENAMED
|
@@ -7511,19 +7511,19 @@ a.anchor-link {
|
|
|
7511
7511
|
<!-- End of mermaid configuration --></head>
|
|
7512
7512
|
<body class="jp-Notebook" data-jp-theme-light="true" data-jp-theme-name="JupyterLab Light">
|
|
7513
7513
|
<main>
|
|
7514
|
-
<div class="jp-Cell jp-MarkdownCell jp-Notebook-cell" id="cell-id=
|
|
7514
|
+
<div class="jp-Cell jp-MarkdownCell jp-Notebook-cell" id="cell-id=6fc59e52">
|
|
7515
7515
|
<div class="jp-Cell-inputWrapper" tabindex="0">
|
|
7516
7516
|
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
|
|
7517
7517
|
</div>
|
|
7518
7518
|
<div class="jp-InputArea jp-Cell-inputArea"><div class="jp-InputPrompt jp-InputArea-prompt">
|
|
7519
7519
|
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput" data-mime-type="text/markdown">
|
|
7520
7520
|
<h1 id="Hello-World!">Hello World!<a class="anchor-link" href="#Hello-World!">¶</a></h1><p>Comments will be rendered as <em>Markdown</em>.
|
|
7521
|
-
The
|
|
7521
|
+
The <code>+</code> and <code>-</code> below ensure that only one code cell is produced,
|
|
7522
7522
|
even though the lines are not contiguous</p>
|
|
7523
7523
|
</div>
|
|
7524
7524
|
</div>
|
|
7525
7525
|
</div>
|
|
7526
|
-
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell" id="cell-id=
|
|
7526
|
+
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell" id="cell-id=bbe7eb3d">
|
|
7527
7527
|
<div class="jp-Cell-inputWrapper" tabindex="0">
|
|
7528
7528
|
<div class="jp-Collapser jp-InputCollapser jp-Cell-inputCollapser">
|
|
7529
7529
|
</div>
|
|
@@ -7536,7 +7536,7 @@ even though the lines are not contiguous</p>
|
|
|
7536
7536
|
<span class="sd"> This demonstrates how larger blocks of code can be built compositionally.</span>
|
|
7537
7537
|
<span class="sd"> """</span>
|
|
7538
7538
|
<span class="k">if</span> <span class="n">temp_c</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
|
|
7539
|
-
<span class="nb">print</span><span class="p">(</span><span class="
|
|
7539
|
+
<span class="nb">print</span><span class="p">(</span><span class="s2">"It is freezing!"</span><span class="p">)</span>
|
|
7540
7540
|
|
|
7541
7541
|
|
|
7542
7542
|
<span class="n">freeze_warning</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">)</span>
|
{dp_wizard_templates-0.1.0/README_examples → dp_wizard_templates-0.2.0/examples}/hello-world.ipynb
RENAMED
|
@@ -2,20 +2,22 @@
|
|
|
2
2
|
"cells": [
|
|
3
3
|
{
|
|
4
4
|
"cell_type": "markdown",
|
|
5
|
-
"id": "
|
|
6
|
-
"metadata": {
|
|
5
|
+
"id": "6fc59e52",
|
|
6
|
+
"metadata": {
|
|
7
|
+
"lines_to_next_cell": 2
|
|
8
|
+
},
|
|
7
9
|
"source": [
|
|
8
10
|
"# Hello World!\n",
|
|
9
11
|
"\n",
|
|
10
12
|
"Comments will be rendered as *Markdown*.\n",
|
|
11
|
-
"The
|
|
13
|
+
"The `+` and `-` below ensure that only one code cell is produced,\n",
|
|
12
14
|
"even though the lines are not contiguous"
|
|
13
15
|
]
|
|
14
16
|
},
|
|
15
17
|
{
|
|
16
18
|
"cell_type": "code",
|
|
17
19
|
"execution_count": 1,
|
|
18
|
-
"id": "
|
|
20
|
+
"id": "bbe7eb3d",
|
|
19
21
|
"metadata": {},
|
|
20
22
|
"outputs": [
|
|
21
23
|
{
|
|
@@ -32,7 +34,7 @@
|
|
|
32
34
|
" This demonstrates how larger blocks of code can be built compositionally.\n",
|
|
33
35
|
" \"\"\"\n",
|
|
34
36
|
" if temp_c < 0:\n",
|
|
35
|
-
" print(
|
|
37
|
+
" print(\"It is freezing!\")\n",
|
|
36
38
|
"\n",
|
|
37
39
|
"\n",
|
|
38
40
|
"freeze_warning(-10)"
|