Skip to content

Commit d72dbeb

Browse files
author
gdj0nes
committed
ADD: code for converting and notebooks
1 parent cdc42c8 commit d72dbeb

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

examples/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
4+
5+
convert:
6+
python scripts/tutorials.py --config-path 'scripts/config.json'

examples/scripts/config.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"dir": "basics",
4+
"title": "Basics",
5+
"order": 0
6+
},
7+
{
8+
"dir": "project_configuration",
9+
"title": "Project configuration",
10+
"order": 1
11+
},
12+
{
13+
"dir": "annotation_types",
14+
"title": "Annotation types",
15+
"order": 2
16+
},
17+
{
18+
"dir": "model_assisted_labeling",
19+
"title": "Model assisted labeling",
20+
"order": 3
21+
},
22+
{
23+
"dir": "label_export",
24+
"title": "Label export",
25+
"order": 4
26+
},
27+
{
28+
"dir": "model_diagnostics",
29+
"title": "Model diagnostics",
30+
"order": 5
31+
},
32+
{
33+
"dir": "integrations",
34+
"title": "Integrations",
35+
"order": 6
36+
}
37+
]

examples/scripts/tutorials.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import glob
2+
import json
3+
import os
4+
5+
import click
6+
import nbformat
7+
import requests
8+
from nbconvert import MarkdownExporter
9+
10+
README_AUTH = os.getenv('README_AUTH')
11+
README_ENDPOINT = "https://dash.readme.com/api/v1/docs"
12+
README_DOC_ENDPOINT = "https://dash.readme.com/api/v1/docs/"
13+
CATEGORY_ID = '61fb645198ad91004246bd5f'
14+
CATEGORY_SLUG = 'tutorials'
15+
16+
17+
def upload_doc(path, section):
18+
title = path.split('/')[-1].replace(".ipynb", '').capitalize().replace('_', ' ')
19+
20+
with open(path) as fb:
21+
nb = nbformat.reads(json.dumps(json.load(fb)), as_version=4)
22+
23+
nb.cells = [nb.cells[1]] + nb.cells[3:]
24+
nb.cells[0]['source'] += '\n'
25+
exporter = MarkdownExporter()
26+
27+
body, resources = exporter.from_notebook_node(nb)
28+
29+
payload = {
30+
"hidden": True,
31+
"title": title.replace(' ', '-') + f'-{section["slug"]}',
32+
"category": CATEGORY_ID,
33+
"parentDoc": section['id'],
34+
"body": body
35+
}
36+
37+
headers = {
38+
"Accept": "application/json",
39+
"Content-Type": "application/json",
40+
"Authorization": README_AUTH
41+
}
42+
43+
response = requests.post(README_ENDPOINT, json=payload, headers=headers)
44+
response.raise_for_status()
45+
data = response.json()
46+
change_name(data['slug'], title, headers)
47+
48+
49+
def make_sections(sections):
50+
for section in sections:
51+
print(section)
52+
payload = {
53+
"hidden": True,
54+
"order": section['order'],
55+
"title": section['title'].replace(' ', '-') + '-nb-section',
56+
"category": CATEGORY_ID
57+
}
58+
headers = {
59+
"Accept": "application/json",
60+
"Content-Type": "application/json",
61+
"Authorization": README_AUTH
62+
}
63+
64+
response = requests.post(README_ENDPOINT, json=payload, headers=headers)
65+
data = response.json()
66+
67+
section['id'] = data['id']
68+
section['slug'] = data['slug']
69+
70+
change_name(data["slug"], section['title'], headers)
71+
72+
return sections
73+
74+
75+
def change_name(slug, title, headers):
76+
resp = requests.put(
77+
f'{README_DOC_ENDPOINT}/{slug}',
78+
json={
79+
"title": title,
80+
"category": CATEGORY_ID
81+
},
82+
headers=headers
83+
)
84+
resp.raise_for_status()
85+
86+
87+
def erase_category_docs(cat_slug):
88+
headers = {
89+
"Accept": "application/json",
90+
"Authorization": README_AUTH
91+
}
92+
93+
response = requests.request("GET", f'https://dash.readme.com/api/v1/categories/{cat_slug}/docs', headers=headers)
94+
docs = response.json()
95+
for doc in docs:
96+
for child in doc["children"]:
97+
resp = requests.delete(f'{README_DOC_ENDPOINT}/{child["slug"]}', headers=headers)
98+
resp = requests.delete(f'{README_DOC_ENDPOINT}/{doc["slug"]}', headers=headers)
99+
100+
101+
@click.command()
102+
@click.option('--config-path')
103+
# @click.option('--output-path')
104+
def main(config_path):
105+
# print(input_path)
106+
erase_category_docs(CATEGORY_SLUG)
107+
with open(config_path) as fb:
108+
config = json.load(fb)
109+
config = make_sections(config)
110+
111+
for section in config:
112+
print(section, '\n------')
113+
for path in glob.glob(f'{section["dir"]}/**.ipynb'):
114+
print('*', path)
115+
upload_doc(path, section)
116+
print('-------')
117+
118+
119+
if __name__ == '__main__':
120+
main()

0 commit comments

Comments
 (0)