[PYTHON] Mutual conversion between Qiita articles and Jupyter notebook

Introduction

When I want to see Qiita's article on Jupyter, I can copy it, but I made a program so that it can be converted all at once.

From Jupyter notebook to Markdown

――First, let's convert from Jupyter notebook to Markdown. --You can also do it from "Download as" in Jupyter's "File" menu, but I'll try it in Python. --The following code is "make_md.py", and if you do the following, the result will be displayed on the screen. --python make_md.py "filename" --In case of "raw", it is enclosed in pre tag. --For "code", it is "py3".

make_md.py


import sys, yaml
if len(sys.argv) < 2:
    exit()

try:
    with open(sys.argv[1], encoding='utf-8') as f:
        ls = yaml.load(f)['cells']
except:
    exit()

for dc in ls:
    typ = dc['cell_type']
    src = ''.join(dc['source'])
    if not src: continue
    if typ == 'markdown':
        print('%s'%src)
    elif typ == 'raw':
        print('<pre>\n%s\n</pre>'%src)
    elif typ == 'code':
        print('\n```py3:python3\n%s\n```'%src)

From Markdown to Jupyter

--The following code is "make_ipynb.py", and the following will create a Jupyter file. - python make_ipynb.py "URL"

make_ipynb.py


import sys, urllib.request
from itertools import takewhile
if len(sys.argv) < 2:
    exit()
try:
    s = urllib.request.urlopen(sys.argv[1]+'.md').read().decode().rstrip()
    ss = s.replace('\\', '\\\\').replace('\t', '\\t').replace('"', '\\"').split('\n')
except:
    exit()
fn = ss[0]
ss[0] = '# ' + ss[0]

def parse_str(ss):
    tt = list(takewhile(lambda s: not s.startswith('```'), ss))
    ss = ss[len(tt):]
    cell_type = 'raw' if len(tt) == 0 else 'markdown'
    if cell_type == 'raw':
        nm = ss[0][(ss[0]+':').index(':')+1:]
        tg = ss[0][3:len(ss[0])-len(nm)].rstrip(':')
        ss = ss[1:]
        if tg.startswith('py') or tg == 'bash':
            cell_type = 'code'
        tt = list(takewhile(lambda s: not s.startswith('```'), ss))
        ss = ss[len(tt):]
        if cell_type == 'code':
            tt = list(takewhile(lambda s: not s.startswith('>>>'), tt))
        if ss:
            ss = ss[1:]
        tt = ([('%%' if tg == 'bash' else '# ') + tg] if tg else []) + tt
    return cell_type, tt, ss
cdin = '   "execution_count": null,\n   "outputs": [],\n   '

rr = []
while ss:
    cell_type, tt, ss = parse_str(ss)
    s = '\\n",\n    "'.join(tt)
    if s:
        rr.append("""\
   "cell_type": "%s",
   "metadata": {},
%s   "source": [
    "%s"
   ]
"""%(cell_type, '' if cell_type != 'code' else cdin, '\\n",\n    "'.join(tt)))

with open(fn+'.ipynb', 'w') as fp:
    fp.write("""\
{
 "cells": [
  {
""")
    fp.write('  },\n  {\n'.join(rr))
    fp.write("""\
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}\n""")

Postscript

--Saito Tsutimu's Python article The last 20 are automatically displayed. - https://hub.docker.com/r/tsutomu7/qiita-jupyter/ --The portal flask port is set to 5000. --The sub Jupyter port is set to 8888. --For Kitematic, return to 8888. --Temporarily released on Arukas. - https://qiita-jupyter.arukascloud.io/

that's all

Recommended Posts

Mutual conversion between Qiita articles and Jupyter notebook
Mutual conversion between fixed point and binary numbers
(Bottom) Cooperation between Jupyter Notebook and running Excel
Mutual conversion between date and days elapsed from January 1, 2000
Mutual conversion between JSON and YAML / TOML in Python
Conversion between unixtime and datetime
Jupyter Notebook: 4 banal tips and tricks
Markdown to get Jupyter notebook results to Qiita
Use Jupyter Lab and Jupyter Notebook with EC2
Jupyter Notebook basic operations and shortcut keys
Linking python and JavaScript with jupyter notebook
Conversion between singular and plural of words
Relationship between Firestore and Go data type conversion
Conversion between Julian day and Gregorian calendar date
[Python] Conversion memo between time data and numerical data
Jupyter Notebook memo
Introducing Jupyter Notebook
Powerful Jupyter Notebook
Jupyter notebook password
Jupyter Notebook memo
Easily launch jupyter notebook on AWS and access locally
Automatically save .py and .html files in Jupyter notebook.
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
Save and manage Qiita articles in Zenn format Markdown
Make Jupyter Notebook your own: Change background and icons