dexplo / jupyter_to_medium

Python package for publishing Jupyter Notebooks as Medium blogposts
https://dexplo.org/jupyter_to_medium
MIT License
144 stars 13 forks source link

Something wrong with working directories #44

Closed protw closed 1 year ago

protw commented 2 years ago

Thanks for quite useful apps.

My OS - Windows 10. For my apps I use virtual environment named airscape. Also I modified jupyter_notebooke_config.py:

c.NotebookApp.notebook_dir = r"D:\boa_uniteam\DOCSIFY"
c.ContentsManager.root_dir = r"D:\boa_uniteam\DOCSIFY"

When I launch the apps from a note book I obtain the following error:

Failed to post to Medium
Returned error message below

"FileNotFoundError: [Errno 2] No such file or directory: 'airscape\\\\stavok\\\\interact_data_demo.ipynb'

Traceback (most recent call last):
File \"C:\\Users\\Asus\\miniconda3\\envs\\airscape\\lib\\site-packages\\nbformat\\__init__.py\", line 138, in read
buf = fp.read()
AttributeError: 'WindowsPath' object has no attribute 'read'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File \"C:\\Users\\Asus\\miniconda3\\envs\\airscape\\lib\\site-packages\\jupyter_to_medium\\_bundler.py\", line 37, in upload
data = publish(**kwargs)
File \"C:\\Users\\Asus\\miniconda3\\envs\\airscape\\lib\\site-packages\\jupyter_to_medium\\_publish_to_medium.py\", line 297, in publish
p = Publish(filename, integration_token, pub_name, title, tags,
File \"C:\\Users\\Asus\\miniconda3\\envs\\airscape\\lib\\site-packages\\jupyter_to_medium\\_publish_to_medium.py\", line 41, in __init__
self.nb = self.get_notebook()
File \"C:\\Users\\Asus\\miniconda3\\envs\\airscape\\lib\\site-packages\\jupyter_to_medium\\_publish_to_medium.py\", line 76, in get_notebook
return nbformat.read(self.filename, as_version=4)
File \"C:\\Users\\Asus\\miniconda3\\envs\\airscape\\lib\\site-packages\\nbformat\\__init__.py\", line 140, in read
with io.open(fp, encoding='utf-8') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'airscape\\\\stavok\\\\interact_data_demo.ipynb'

After that I tried the command line launch from my own working directory. Surprisingly, but it worked, though without saving images in the draft. Probably it worthwhile to mention that in this notebook I use for vizualization Holoviz with Bokeh beckend.

mjam03 commented 2 years ago

Hi,

Thanks for using the app! I've had a quick look and have traced through the above error message and code. In order for jupyter_to_medium to upload your notebook to medium it needs to:

It seems like it is failing on the first hurdle - identifying where the .ipynb file sits so it can convert it to .md etc etc. From looking through the code this is done by the line:

return nbformat.read(self.filename, as_version=4)

in the get_notebook method where the filename attribute is passed down by the bundler - the bit that connects this python code to the jupyter notebook instance. All in all I have no idea why the bundler is passing down the wrong file location and will struggle to replicate, however the fact that it works when running from the command line is re-assuring as that means the issue does appear to be the bundler.

I also run this within virtual environments (either venv or conda envs) and it works no problem. Are you running your instance of Jupyter inside your venv?

protw commented 2 years ago

Thanks for your time and efforts. It seems I found the reason - as I told before for my apps I modified configuration code C:\Users\my_user_name\.jupyter\jupyter_notebooke_config.py uncommenting two lines as follow:

c.NotebookApp.notebook_dir = r"D:\boa_uniteam\DOCSIFY"
c.ContentsManager.root_dir = r"D:\boa_uniteam\DOCSIFY"

In this way I established another than default working directory, which is more convenient for me.

After I've commented these lines again and moved my ipynb file to the default directory C:\Users\my_user_name\ your application started working immediately.

It's great. Thanks. Though I would prefer working in an arbitrary directory where my ipynb files originally located.

However, there is another part of the story. Even after having started working your apps does not render my diagrams. I suspect though am not sure that's because I use holoviz graphics package. My code is straightforward and you could see it at https://github.com/protw/airscape/blob/master/stavok/interact_data_demo.ipynb

mjam03 commented 1 year ago

Hey,

Sorry for the year long delay on this - yep that is probably the issue. Each new visualisation package requires integration in order to screenshot the image for Medium upload as PNG.

Going to close this issue for now - holoviz isn't a widely used package (compared to e.g.matplotlib) so it's not worth the effort of integrating. Although if you'd like to make a PR that would be amazing!

Mark