A Reddit bot to convert GitHub Jupyter Notebook URLs to nbviewer links for more consistent notebook rendering, and to generate binder links to run notebooks yourself.
jd_paton:
Check out my cool notebook: https://github.com/JohnPaton/numpy-neural-networks/blob/master/01-single-layer-perceptron.ipynb
nbviewerbot:
I see you've posted a GitHub link to a Jupyter Notebook! GitHub doesn't render large Jupyter Notebooks, so just in case, here is an nbviewer link to the notebook:
Want to run the code yourself? Here is a binder link to start your own Jupyter server and try it out!
To install nbviewerbot
, first clone this repository, and then install it in edit mode using pip
:
$ git clone [email protected]:JohnPaton/nbviewerbot.git
$ cd nbviewerbot
$ pip install -e .
Before running, you must register a script application with Reddit. You must also give this application access to an account for the bot to post from. Please follow the instructions here. Once you have done this, you must store the account's username and password, and your app's client id and client secret in a .env
file in the package's nbviewerbot
directory. A template for this file is provided at nbviewerbot/.env_template
. Note: If you do not install in edit mode (i.e. without pip's -e
option) then you must reinstall the package (pip install --upgrade .
) every time you change the nbviewerbot/.env
file, or change it directly in your system's install location. You can also provide a custom .env
file at runtime (see Usage).
If you want to run the tests, you can do so with pytest
(for your current environment) or tox
(for available python3
environments).
Once you have pip
installed nbviewerbot
, you can run it from the command line with the command nbviewerbot
. This will watch a default list of relevant subreddits for comments containing GitHub links to Jupyter Notebooks, and reply to them with a comment containing a nbviewer link for each Jupyter link in the parent.
$ nbviewerbot
2019-08-25 13:26:43,502 INFO(MainThread) - Successfully authenticated with Reddit
2019-08-25 13:26:43,503 INFO(MainThread) - Streaming comments from algotrading+analytics+apachespark+arcgis+artificial+askmath+askprogramming+aws+beginnerprojects+bigdata+bioinformatics+businessintelligence+codeprojects+computerscience+computervision+coolgithubprojects+cs231n+cscareerquestions+datacleaning+datamining+datascience+datasciencenews+deepdream+deeplearners+deeplearning+gis+github+investing+ipynb+ipython+jupyter+jupyternotebooks+languagetechnology+learnmachinelearning+learnprogramming+learnpython+machinelearning+mlquestions+nlp+physics+programming+pyfinance+pystats+python+pythontips+qgis+reinforcementlearning+rstats+science+simulate+statistics+tensorflow+testingground4bots+bottestingplace+bottesting+bottest
2019-08-25 13:26:43,522 INFO(MainThread) - Started nbviewerbot, listening for new comments...
2019-08-25 13:26:46,077 INFO(MainThread) - Found Jupyter link(s) in comment ey29inb
2019-08-25 13:26:46,376 INFO(MainThread) - Replied to comment ey29inb with new comment ey2ab70
You can stop the bot again by pressing ctrl+C
. This will interrupt the loop, prompting the bot to save its state (preventing it from replying to the same comments again on next startup) and exit.
^C
2019-08-25 14:23:40,268 WARNING(MainThread) - Stopping nbviewerbot...
2019-08-25 14:23:46,928 INFO(CommentWorker) - Stop signal received, stopping
2019-08-25 14:23:47,109 INFO(SubmissionWorker) - Stop signal received, stopping
2019-08-25 14:23:47,113 INFO(Dummy-1) - Saving reply log...
2019-08-25 14:23:47,169 INFO(Dummy-1) - Exited nbviewerbot
To view the available subreddit lists, use the command nbviewerbot subreddits
. The default list is the testing list plus a list of relevant subreddits. Additions to this list would be welcome, feel free to open a PR!
For more details on the command line interface, please use the --help
argument:
$ nbviewerbot --help
Usage: nbviewerbot [OPTIONS] COMMAND [ARGS]...
Run the nbviewerbot on the selected subreddit set.
Options:
-v, --verbose Show DEBUG logs (always available at
/path/to/nbviewerbot/nbviewerbot.log)
-q, --quiet Don't show any command line output
-s, --subreddit-set [relevant|test|all]
Set of subreddits to use. Options:
"relevant" (default, only relevant
subreddits), "all" (/r/all), or "test" (bot
testing subreddits). Use the "nbviewerbot
subreddits" command to view the complete
lists.
-e, --env PATH A custom .env file for loading environment
variables. Relevant vars: CLIENT_ID,
CLIENT_SECRET, USERNAME, PASSWORD.
--help Show this message and exit.
Commands:
subreddits Show subreddits used by the -s options
The included startup.sh
will start a screen
session for nbviewerbot
which will keep the bot alive continuously. However, for something a bit more robust to
e.g. restarts, systemd should be used instead. The exact details will depend on your
installation method, but if you have installed in a virutal environment and want to
use a .env
file for your credentials, this should get you started:
[Unit]
Description=nbviewerbot
[Service]
User=<YOUR_USERNAME>
Environment="VIRTUAL_ENV=/absolute/path/to/the/virtualenv"
Environment="DOTENV=/absolute/path/to/the/.env"
ExecStart=/bin/bash -c 'source "${VIRTUAL_ENV}/bin/activate" && nbviewerbot -e "${DOTENV}"'
Restart=on-failure
RestartSec=15 # Avoid getting rate limited by frequent restarts
[Install]
WantedBy=multi-user.target
# Don't try to start unless we have network connectivity
After=network-online.target
Wants=network-online.target
You should save or link this file to /etc/systemd/system/nbviewerbot.service
, and then
enable it with
$ sudo systemctl enable nbviewerbot.service
$ sudo systemctl start nbviewerbot.service
You can check the logs with systemctl status nbviewerbot.service
or
journalctl -u nbviewerbot.service
.