Skip to content

Commit 66d66aa

Browse files
authored
Merge pull request #70 from dexplo/develop
Develop
2 parents aa0b59a + 541eb5a commit 66d66aa

14 files changed

+215
-149
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ jtm.publish('My Awesome Jupyter Notebook.ipynb',
114114
save_markdown=False,
115115
table_conversion='chrome',
116116
gistify=False,
117-
gist_threshold=5
117+
gist_threshold=5,
118+
public_gists=True
118119
)
119120
```
120121

docs/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ jtm.publish('My Awesome Jupyter Notebook.ipynb',
133133
save_markdown=False,
134134
table_conversion='chrome',
135135
gistify=False,
136-
gist_threshold=5
136+
gist_threshold=5,
137+
public_gists=True
137138
)
138139
```
139140

docs/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
markdown-include==0.6.0
2-
mkdocs==1.2.3
2+
mkdocs==1.2.4
33
mkdocs-autolinks-plugin==0.4.0
44
mkdocs-autorefs==0.3.0
55
mkdocs-git-revision-date-localized-plugin==0.10.1

poetry.lock

+48-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "jupyter-to-medium"
3-
version = "0.2.11"
3+
version = "0.2.12"
44
description = "Publish a Jupyter Notebook as a Medium blogpost"
55
authors = ["dexplo <[email protected]>"]
66
maintainers = ["mjam03 <[email protected]>"]
@@ -40,6 +40,10 @@ nox = "^2021.10.1"
4040
nox-poetry = "^0.9.0"
4141

4242
# let pytest coverage know where code is
43+
44+
[tool.poetry.group.dev.dependencies]
45+
mypy = "^1.1.1"
46+
4347
[tool.coverage.paths]
4448
source = ["src", "*/site-packages"]
4549

src/jupyter_to_medium/_bundler.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99

1010
def _jupyter_bundlerextension_paths():
11+
"""Function used to install extension when the following is called on comand line:
12+
jupyter bundlerextension enable --py jupyter_to_medium._bundler --sys-prefix
13+
Jupyter docs here:
14+
https://jupyter-notebook.readthedocs.io/en/stable/extending/bundler_extensions.html#enabling-disabling-bundler-extensions
15+
16+
Returns:
17+
list: config needed to install extension
18+
"""
1119
return [
1220
{
1321
# the name of the bundle
@@ -37,18 +45,20 @@ def upload(model, handler):
3745
"table_conversion",
3846
"gistify",
3947
"gist_threshold",
48+
"public_gists"
4049
]
4150

4251
kwargs = {arg: handler.get_query_argument(arg, None) for arg in arguments}
4352
path = model["path"]
4453
kwargs["filename"] = path
4554
kwargs["integration_token"] = kwargs["integration_token"].strip() or None
46-
kwargs["pub_name"] == kwargs["pub_name"].strip() or None
55+
kwargs["pub_name"] = kwargs["pub_name"].strip() or None
4756
kwargs["tags"] = [tag.strip() for tag in kwargs["tags"].split(",")[:5]]
4857
kwargs["notify_followers"] = kwargs["notify_followers"] == "True"
4958
kwargs["canonical_url"] = kwargs["canonical_url"].strip() or None
5059
kwargs["save_markdown"] = kwargs["save_markdown"] == "True"
5160
kwargs["gistify"] = kwargs["gistify"] == "True"
61+
kwargs["public_gists"] = kwargs["public_gists"] == "True"
5262
if kwargs["gist_threshold"] == "":
5363
kwargs["gist_threshold"] = 5
5464
else:
@@ -85,7 +95,7 @@ def upload(model, handler):
8595
def read_html(name):
8696
mod_path = Path(__file__).parent
8797
html_path = mod_path / "static" / f"{name}.html"
88-
return open(html_path).read()
98+
return open(html_path, encoding="utf-8").read()
8999

90100

91101
def get_html_form(xsrf_input, title):

src/jupyter_to_medium/_command_line.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ class CustomFormatter(argparse.RawTextHelpFormatter):
106106
generate a Personal Access Token (PAT) on github that is then used,
107107
similar to the Medium Integration Token, to create the gists.
108108
109-
--gist_threshold
109+
--gist-threshold
110110
If chosen to use gists for code blocks, this sets the length in lines of
111111
code for which to make code blocks into gists. This is to prevent gists of
112112
only several lines unless desired.
113+
114+
--public-gists
115+
Whether to create the gists as public (can be found by search engines)
116+
or private (only accessible through link).
113117
114118
Examples
115119
========
@@ -120,18 +124,14 @@ class CustomFormatter(argparse.RawTextHelpFormatter):
120124
121125
"""
122126

123-
parser = argparse.ArgumentParser(
124-
formatter_class=CustomFormatter, add_help=False, usage=argparse.SUPPRESS
125-
)
127+
parser = argparse.ArgumentParser(formatter_class=CustomFormatter, add_help=False, usage=argparse.SUPPRESS)
126128
parser.add_argument("filename", default=False)
127129
parser.add_argument("-h", "--help", action="store_true", dest="help")
128130
parser.add_argument("--integration-token", type=str)
129131
parser.add_argument("--pub-name", type=str)
130132
parser.add_argument("--title", type=str)
131133
parser.add_argument("--tags", type=str)
132-
parser.add_argument(
133-
"--publish-status", type=str, choices=["draft"], default="draft"
134-
)
134+
parser.add_argument("--publish-status", type=str, choices=["draft"], default="draft")
135135
parser.add_argument("--notify-followers", type=bool, default=False)
136136
parser.add_argument(
137137
"--license",
@@ -160,6 +160,7 @@ class CustomFormatter(argparse.RawTextHelpFormatter):
160160
)
161161
parser.add_argument("--gistify", type=bool, default=False)
162162
parser.add_argument("--gist-threshold", type=int, default=5)
163+
parser.add_argument("--public-gists", type=bool, default=False)
163164

164165

165166
def main():

src/jupyter_to_medium/_latex.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ def is_latex_cell(cell):
3030

3131

3232
def render_latex(formula, fontsize=10, dpi=200, format_="png"):
33-
"""Renders LaTeX formula into image.
34-
"""
33+
"""Renders LaTeX formula into image."""
3534
# set formatting
3635
mpl_context = {
3736
"text.usetex": False,
@@ -43,7 +42,7 @@ def render_latex(formula, fontsize=10, dpi=200, format_="png"):
4342
# use formatting and make text figure
4443
with plt.style.context(mpl_context):
4544
fig = plt.figure(figsize=(0.01, 0.01))
46-
fig.text(0, 0, u"${}$".format(formula), fontsize=fontsize)
45+
fig.text(0, 0, f"${formula}$", fontsize=fontsize)
4746
# create bytes buffer
4847
buffer_ = StringIO()
4948
# save fig to buffer and close it
@@ -158,13 +157,13 @@ def format_multiline_latex(latex: list) -> str:
158157
def format_latex(latex: str) -> str:
159158

160159
# seperate handling for one liners vs multi-liners
161-
latex = latex.split("\n")
162-
if len(latex) == 1:
160+
latex_list = latex.split("\n")
161+
if len(latex_list) == 1:
163162
# then we have a 1 liner
164-
lt = format_single_line_latex(latex[0])
163+
lt = format_single_line_latex(latex_list[0])
165164
else:
166165
# else multiline statement
167-
lt = format_multiline_latex(latex)
166+
lt = format_multiline_latex(latex_list)
168167
return lt
169168

170169

src/jupyter_to_medium/_matplotlib_table.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def parse_html(self, html):
3737
num_cols = sum(val[-1] for val in rows[0])
3838
new_rows = []
3939
rowspan = {}
40-
for i, row in enumerate(rows):
40+
for _, row in enumerate(rows):
4141
new_row = []
4242
col_loc = 0
4343
j = 0
@@ -123,9 +123,7 @@ def get_all_text_widths(self, rows):
123123
for vals in row:
124124
cell_max_width = 0
125125
for text in vals[0].split("\n"):
126-
cell_max_width = max(
127-
cell_max_width, self.get_text_width(text)
128-
)
126+
cell_max_width = max(cell_max_width, self.get_text_width(text))
129127
row_widths.append(cell_max_width)
130128
all_text_widths.append(row_widths)
131129
return np.array(all_text_widths) + 15
@@ -162,14 +160,12 @@ def wrap_col(self, idx, col_widths, mult):
162160
texts = [row[idx][0] for row in self.rows]
163161
new_texts = []
164162
new_max_width = 0
165-
for i, (text, col_width) in enumerate(zip(texts, col_widths)):
163+
for _, (text, col_width) in enumerate(zip(texts, col_widths)):
166164
if col_width > mult * max_width and len(text) > self.wrap_length:
167165
width = max(self.wrap_length, int(len(text) * mult))
168166
new_text = textwrap.fill(text, width, break_long_words=False)
169167
new_texts.append(new_text)
170-
new_max_width = max(
171-
new_max_width, self.get_text_width(new_text)
172-
)
168+
new_max_width = max(new_max_width, self.get_text_width(new_text))
173169
else:
174170
new_texts.append(text)
175171

@@ -243,12 +239,10 @@ def print_table(self):
243239
self.fig.add_artist(p)
244240

245241
if i == self.num_header_rows - 1:
246-
line = mlines.Line2D(
247-
[x0, x0 + total_width], [y, y], color="black"
248-
)
242+
line = mlines.Line2D([x0, x0 + total_width], [y, y], color="black")
249243
self.fig.add_artist(line)
250244

251-
w, h = self.fig.get_size_inches()
245+
_, h = self.fig.get_size_inches()
252246
start = self.figwidth * min(x0, 0.1)
253247
end = self.figwidth - start
254248
bbox = Bbox([[start - 0.1, y * h], [end + 0.1, h]])

0 commit comments

Comments
 (0)