converting from .py to .ipynb

对着背影说爱祢 提交于 2020-12-13 03:12:32

问题


I wrote a juypter notebook that has been converted to .py somehow. I would like it back in the original format. Does anyone know how to do that?

There is a previous stack overflow question about this, but the solution doesn't work for me. Converting to (not from) ipython Notebook format

The below is an example of what the code looks like now. It is a lot of code so would take hours to copy and paste it manually.

Thanks for the help.

{
   "cell_type": "code",
   "execution_count": 581,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_trig_slope(data, size = 1, axis = 0, option = 0, random = False, lower_b = -1, upper_b = 2): \n",
    "    \n",
    "    # To make the gradual decline of the fuck you plot\n",
    "    ## sin, cos, tan, sigmoid, other activation functions?\n",
    "    # need to list the option in the doc string\n",
    "    \n",
    "    ## Add a random element\n",
    "    newdata = data.copy()\n",
    "    cols = list(newdata.columns)\n",
    "    funcs = [math.sin, math.cos, math.tan, expit]\n",
    "    func = funcs[option]\n",
    "    if axis == 0:\n",
    "        for col in cols:\n",
    "            newdata.loc[:, col] -= size * (func(cols.index(col)))\n",
    "            if random:\n",
    "                newdata.loc[:,col] -= np.random.uniform(lower_b,upper_b)\n",
    "\n",
    "    elif axis == 1:\n",
    "        for i, node in enumerate(newdata.index):\n",
    "            newdata.loc[node,:] -= size * (func(i))\n",
    "            if random:\n",
    "                newdata.loc[node,:] -= np.random.uniform(lower_b,upper_b)\n",
    "\n",
    "        \n",
    "    return newdata\n",
    "    \n",
    "    "
   ]


回答1:


Just rename it changing the extension e.g. for linux/macos

mv <file>.py <file>.ipynb

or right-click rename for windows and type the full name with the extension

(Since it seems that the contents are .ipynb contents already)




回答2:


Use p2j to convert Python source code to Jupyter Notebook.

From the command line, run

-->pip install p2j

then go to the directory where your file is located. -->( for example-> cd downloads, if the file is in download directory)

then run

-->p2j myscript.py

This will create a myscript.ipynb file.



来源:https://stackoverflow.com/questions/62510114/converting-from-py-to-ipynb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!