Yuki-Inoue / jupyter_on_rails

Integrate Rails and Jupyter
MIT License
83 stars 11 forks source link

Pass flags to `rake jupyter:notebook` #29

Open jorge0136 opened 4 years ago

jorge0136 commented 4 years ago

I am attempting to add jupyter_on_rails to an existing rails application in a docker container. When I execute rake jupyter:notebook from within my container I get the following error. It appears that it is an instance of this issue and may well be an issue with the way I have my container configured.

Running jupyter via iruby notebook --port 8888 --ip=0.0.0.0 starts the server ok but I am unable to require 'iruby/rails' within a notebook. Do you have any suggestions for how to pipe flags into the rake task or otherwise troubleshoot this issue?

$ rake jupyter:notebook
JUPYTER_DATA_DIR=/app/.ipython bundle exec iruby register --force
rm -rf /app/.ipython/kernels/rails
cp -r /app/.ipython/kernels/ruby /app/.ipython/kernels/rails
Traceback (most recent call last):
  File "/usr/local/bin/jupyter-notebook", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/dist-packages/jupyter_core/application.py", line 270, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 663, in launch_instance
    app.initialize(argv)
  File "<decorator-gen-7>", line 2, in initialize
  File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/notebook/notebookapp.py", line 1769, in initialize
    self.init_webapp()
  File "/usr/local/lib/python3.7/dist-packages/notebook/notebookapp.py", line 1490, in init_webapp
    self.http_server.listen(port, self.ip)
  File "/usr/local/lib/python3.7/dist-packages/tornado/tcpserver.py", line 151, in listen
    sockets = bind_sockets(port, address=address)
  File "/usr/local/lib/python3.7/dist-packages/tornado/netutil.py", line 174, in bind_sockets
    sock.bind(sockaddr)
OSError: [Errno 99] Cannot assign requested address
--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/92354434-pass-flags-to-rake-jupyter-notebook?utm_campaign=plugin&utm_content=tracker%2F123687093&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F123687093&utm_medium=issues&utm_source=github).
jorge0136 commented 4 years ago

The more I look at this error the less it seems to have anything to do with jupyter_on_rails and more likely my container networking config is to blame. Do you have any suggestions on how to handle the use case where one might want to pass flags to the jupyter command?

I haven't ever found a smooth and easy way to pass flags through rake without making each of them explicit.

$ rake jupyter:notebook --port 8888
invalid option: --port
jorge0136 commented 4 years ago

For a local development workaround, I was able to reconstruct the command the rake task is executing.

First I evaluated ENV['IPYTHONDIR'] || Rails.root / '.ipython' in the rails console. I set the JUPYTER_DATA_DIR env to the output of the command. Finally, I added my required flags. In my case that all shook out to be JUPYTER_DATA_DIR=/app/.ipython jupyter notebook --ip=0.0.0.0 --port 8888.

While this feels like progress, I still haven't solved my root issue. I still get an error when attempting to require 'iruby/rails'. Other ruby gems will load but not this one.

Yuki-Inoue commented 4 years ago

Do you have any suggestions on how to handle the use case where one might want to pass flags to the jupyter command?

I think if we are to do this, an cli command implementation of this jupyter_on_rails would be needed, such that bundle exec juptyer_on_rails notebook --ip=0.0.0.0 --port 8888 which includes the current functionality of rake jupyter:notebook.

Doing that is simply a matter of development; if anyone can implement it, I'd be happy to merge; if I can spare time, I'll probably implement this feature, but for now I'm a bit occupied.

tim37021 commented 3 years ago

I came across this project. Thanks for the life saver.

you can use ssh forwarding for this. If you are using terminal of VSCode remote. It will forward 8888 to you automatically.

elalemanyo commented 1 year ago

@jorge0136 @Yuki-Inoue Jupyter notebooks allow you to set various configuration options using jupyter_notebook_config.py. This file can be located in the Jupyter configuration directory, which is usually ~/.jupyter/.

By setting the options in the jupyter_notebook_config.py file, you do not need to pass flags when starting the Jupyter notebook server from the command line. The options set in the configuration file will be applied every time the Jupyter notebook server is started. This can be particularly useful if you have a set of options that you always want to use, or if you want to make changes to the default behavior of Jupyter notebooks.

For example, you can set the IP and port like this:

# Set the IP address for Jupyter to listen on
c.NotebookApp.ip = '0.0.0.0'

# Set the port for Jupyter to listen on
c.NotebookApp.port = 8888

This is how I use jupyter_on_rails with a rails application running in docker. I hope it helps