├── .gitignore ├── Scripts ├── README.md ├── latex_space_strip.py ├── i2r_n.py └── i2r_p.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /Scripts/README.md: -------------------------------------------------------------------------------- 1 | # ipynb_to_rst 2 | 3 | Aim: Develop a Python tool, which converts ipynb files to rst files. 4 | 5 | ## 1 The files in the folder `tests` are our original jupyter notebooks. 6 | 7 | 1. The file `information_consumption_smoothing-v3.ipynb` == `info.ipynb` 8 | 9 | 2. The file `Inventory_sales_smoothing-v6.ipynb` == `inv.ipynb` 10 | 11 | ## 2 The file `ipynb_to_rst.py` is the version 0.03 of our Python script. 12 | 13 | 1. It enables us to produce a .rst file from a .ipnyb file 14 | 15 | 2. In order to achieve that, after saving this .py file into your directory, you can type the following code in your terminal 16 | 17 | `python ipynb_2_rst.py [filename.ipynb] -o [filename.rst]` 18 | 19 | In our testing examples, we have 20 | 21 | `python ipynb_2_rst.py info.ipynb -o infox.rst` 22 | 23 | and 24 | 25 | `python ipynb_2_rst.py inv.ipynb -o invx.rst` 26 | -------------------------------------------------------------------------------- /Scripts/latex_space_strip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """A simple python script that removes spaces before and 4 | after dollar signs in inline Latex code inside .ipynb files 5 | """ 6 | 7 | import os 8 | import sys 9 | import argparse 10 | import re 11 | 12 | def main(arguments): 13 | 14 | parser = argparse.ArgumentParser( 15 | description=__doc__, 16 | formatter_class=argparse.RawDescriptionHelpFormatter) 17 | parser.add_argument('infile', help="Input file", type=argparse.FileType('r')) 18 | parser.add_argument('-o', '--outfile', help="Output file", 19 | default=sys.stdout, type=argparse.FileType('w+')) 20 | args = parser.parse_args(arguments) 21 | #improve navigation and error catching to file in case user includes directory not in home of file 22 | with args.infile as f: 23 | content=f.read() 24 | with args.outfile as new_f: 25 | new_content=content 26 | new_content=re.sub(r'(\$.*)\s(\$)',r'\1\2', new_content) 27 | new_content=re.sub(r'(\$)\s(.*\$)',r'\1\2', new_content) 28 | new_f.write(new_content) 29 | if __name__ == '__main__': 30 | sys.exit(main(sys.argv[1:])) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ipynb_to_rst_tools 2 | Tools, scripts and instructions for converting Jupyter notebooks to RST files 3 | 4 | ## Instructions from Brandon and Tom 5 | 6 | 7 | ### Conversion method for pandoc 8 | 9 | To load on Manjaro Dell: 10 | 11 | 1. Must have pandoc >= 2.6 12 | 13 | To get it: 14 | 15 | * yay pandoc -bin 16 | 17 | * on ubuntu via package manager or via debian 18 | 19 | Check version via ```pandoc --version [in a terminal]``` 20 | 21 | If you see a lower numbered version of pandoc... 22 | 23 | ```conda deactivate``` (in a terminal) 24 | 25 | **Note:** As of March 2019, the default version of pandoc in Anaconda is 1.19 26 | 27 | To get things to work in a terminal (in the directory where the `*.ipynb` is): 28 | 29 | 1. This step is necessary only if you want to strip out the bad `$ \alpha $` stuff 30 | 31 | In a bash terminal issue command 32 | 33 | ```python latex_space_strip.py [myinputfile.ipynb] -o [myoutputfile.ipynb]``` 34 | 35 | 2. Issue this command in a bash terminal 36 | 37 | ```pandoc [myfilenamenew.pynb] -f ipynb+tex_math_dollars -t rst -s -o [newfilename.rst]``` 38 | 39 | For example (this worked on Feb 22, 2019 in Singapore) 40 | 41 | ```pandoc test23.ipynb -f ipynb+tex_math_dollars -t rst -s -o test23.rst``` 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Scripts/i2r_n.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Version 0.01 for Nbconvert 3 | 4 | """A simple python script that removes spaces before and 5 | after dollar signs in inline Latex code inside .ipynb files 6 | and also produce a .rst file from the new .ipynb file by Using `nbconvert` 7 | """ 8 | 9 | import os 10 | import sys 11 | import argparse # clear 12 | import re 13 | 14 | def main(arguments): 15 | 16 | parser = argparse.ArgumentParser( 17 | description=__doc__, # text to display before the argument help 18 | formatter_class=argparse.RawDescriptionHelpFormatter) 19 | parser.add_argument('infile', help="Input file", type=argparse.FileType('r')) 20 | parser.add_argument('-o', '--outfile', help="Output file", 21 | default=sys.stdout, type=argparse.FileType('w+')) 22 | args = parser.parse_args(arguments) 23 | #improve navigation and error catching to file in case user includes directory not in home of file 24 | with args.infile as f: 25 | content=f.read() 26 | with args.outfile as new_f: 27 | new_content=content 28 | new_content=re.sub(r'(\$.*)\s(\$)',r'\1\2', new_content) 29 | new_content=re.sub(r'(\$)\s(.*\$)',r'\1\2', new_content) 30 | new_f.write(new_content) 31 | os.system('jupyter nbconvert --to rst %s' % (args.outfile.name)) 32 | if __name__ == '__main__': 33 | sys.exit(main(sys.argv[1:])) 34 | -------------------------------------------------------------------------------- /Scripts/i2r_p.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Version 0.04 for Pandoc 3 | 4 | """A simple python script that removes spaces before and 5 | after dollar signs in inline Latex code inside .ipynb files 6 | and also produce a .rst file from the new .ipynb file by Using `Pandoc` 7 | """ 8 | 9 | import os 10 | import sys 11 | import argparse # clear 12 | import re 13 | 14 | def main(arguments): 15 | 16 | parser = argparse.ArgumentParser( 17 | description=__doc__, # text to display before the argument help 18 | formatter_class=argparse.RawDescriptionHelpFormatter) 19 | parser.add_argument('infile', help="Input file", type=argparse.FileType('r')) 20 | parser.add_argument('-m', '--median', help="Intermediate Output file", 21 | default=sys.stdout, type=argparse.FileType('w+')) 22 | parser.add_argument('-o', '--outfile', help="Output file", 23 | default=sys.stdout, type=argparse.FileType('w+')) 24 | args = parser.parse_args(arguments) 25 | #improve navigation and error catching to file in case user includes directory not in home of file 26 | with args.infile as f: 27 | content=f.read() 28 | with args.median as new_f: 29 | new_content=content 30 | new_content=re.sub(r'(\$.*)\s(\$)',r'\1\2', new_content) 31 | new_content=re.sub(r'(\$)\s(.*\$)',r'\1\2', new_content) 32 | new_f.write(new_content) 33 | os.system('pandoc -f ipynb -t rst %s -o %s' % (args.median.name, args.outfile.name)) 34 | if __name__ == '__main__': 35 | sys.exit(main(sys.argv[1:])) 36 | --------------------------------------------------------------------------------