Search code examples
filejupyter-notebookjuliafile-conversion

.ipynb Julia files to .jl files


Is there a convenient way to convert several .ipynb Julia files produced with Jupyter notebook to .jl working files?

It is good if the markdown written with Jupyter are kept but not mandatory for my usage.

Here is sample of what my files contain:

{
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Some useful functions required to build the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "distance (generic function with 1 method)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Euclidean distances between point p1 and p2\n",
    "function distance(p1,p2)\n",
    "    dist=((p1[1]-p2[1])^2+(p1[2]-p2[2])^2)^(1/2)\n",
    "    return dist\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tripletas (generic function with 1 method)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],

Solution

  • There is a very convenient way: jupytext. The docs show how you can use it both inside of jupyter or jupyterlab and at the command line.

    Example:

    jupytext --to jl Notebook.ipynb
    

    from a shell you could do something like:

    jupytext --to jl "path/to/notebooks/*.ipynb"