dp_wizard_templates 0.1.0__py2.py3-none-any.whl → 0.2.0__py2.py3-none-any.whl
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/VERSION +1 -1
- dp_wizard_templates/code_template.py +7 -3
- dp_wizard_templates/converters.py +21 -8
- {dp_wizard_templates-0.1.0.dist-info → dp_wizard_templates-0.2.0.dist-info}/METADATA +4 -8
- dp_wizard_templates-0.2.0.dist-info/RECORD +8 -0
- dp_wizard_templates-0.1.0.dist-info/RECORD +0 -8
- {dp_wizard_templates-0.1.0.dist-info → dp_wizard_templates-0.2.0.dist-info}/WHEEL +0 -0
- {dp_wizard_templates-0.1.0.dist-info → dp_wizard_templates-0.2.0.dist-info}/licenses/LICENSE +0 -0
dp_wizard_templates/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.2.0
|
|
@@ -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
|
|
@@ -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
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
dp_wizard_templates/VERSION,sha256=kR_AxIywxwYB21d1qb7xt0DcTMn5tGOJufBWP-frlNc,5
|
|
2
|
+
dp_wizard_templates/__init__.py,sha256=E2xrnvZGY24pU3PCryH4TmvhNYsLmxXCtfvIcYNbTYw,126
|
|
3
|
+
dp_wizard_templates/code_template.py,sha256=HwnmKmJStkan9jkdyRQ4zVnJcvHWymzzCt8lwdTdtX4,4504
|
|
4
|
+
dp_wizard_templates/converters.py,sha256=voLc2KlJ7WZGYMtA8x0pB1xBcLJs974uoX2nCJO7uD0,4331
|
|
5
|
+
dp_wizard_templates-0.2.0.dist-info/licenses/LICENSE,sha256=FDrIMeZPiT4g_4w0i1Ec4Bc8h9wfNytroheQN4508yU,1063
|
|
6
|
+
dp_wizard_templates-0.2.0.dist-info/WHEEL,sha256=Dyt6SBfaasWElUrURkknVFAZDHSTwxg3PaTza7RSbkY,100
|
|
7
|
+
dp_wizard_templates-0.2.0.dist-info/METADATA,sha256=zvMOoJELX1KP5e_3gIcimNjp62lDNyOd7BfkGahvdLg,1881
|
|
8
|
+
dp_wizard_templates-0.2.0.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
dp_wizard_templates/VERSION,sha256=atlhOkVXmNbZLl9fOQq0uqcFlryGntaxf1zdKyhjXwY,5
|
|
2
|
-
dp_wizard_templates/__init__.py,sha256=E2xrnvZGY24pU3PCryH4TmvhNYsLmxXCtfvIcYNbTYw,126
|
|
3
|
-
dp_wizard_templates/code_template.py,sha256=GW4wcAguGRFEE_eKLPedjmsklZR9dtx4wn8CYq-avh8,4389
|
|
4
|
-
dp_wizard_templates/converters.py,sha256=l-MYAiTAH_isjggH03DKlgV-j-uxc0CyW1hiCLD39yQ,3957
|
|
5
|
-
dp_wizard_templates-0.1.0.dist-info/licenses/LICENSE,sha256=FDrIMeZPiT4g_4w0i1Ec4Bc8h9wfNytroheQN4508yU,1063
|
|
6
|
-
dp_wizard_templates-0.1.0.dist-info/WHEEL,sha256=Dyt6SBfaasWElUrURkknVFAZDHSTwxg3PaTza7RSbkY,100
|
|
7
|
-
dp_wizard_templates-0.1.0.dist-info/METADATA,sha256=ZMvavNpdRK7GvyB-j4ht2JWSzPjhIC52n7VHVCyX70c,2139
|
|
8
|
-
dp_wizard_templates-0.1.0.dist-info/RECORD,,
|
|
File without changes
|
{dp_wizard_templates-0.1.0.dist-info → dp_wizard_templates-0.2.0.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|