{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Jmol Jsmol applets working in Jupyter notebooks\n", "\n", "This notebook shows how to run Jmol/ Jsmol applets in Jupyter notebook a couple of ways. \n", "\n", "For the second one, using jupyter-jsmol, to work you have to run this notebook in sessions launched from [my repo with jupyter-jsmol listed](https://github.com/fomightez/jupyter-jsmol-binder) or [the jupyter-jsmol extension development repo](https://github.com/fekad/jupyter-jsmol).\n", "\n", "If not sorely using the jupyter-jsmol method: \n", "First, separately as another notebook on the same MyBinder session, I ran the first several cells of `Jmol served via a MyBinder remote session.ipynb` to get Jmol and place a directory `jsmol` in the root directory of the running session. **This step is important to put in place the jsmol directory with the javascript that the applet needs.** " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Old(?) versions of these steps are listed below for convenience.\n", "\n", "```bash\n", "!curl -L -o Jmol-14.31.20-binary.tar.gz https://sourceforge.net/projects/jmol/files/Jmol/Version%2014.31/Jmol%2014.31.20/Jmol-14.31.20-binary.tar.gz/download\n", "!tar xzf Jmol-14.31.20-binary.tar.gz\n", "!unzip jmol-14.31.20/jsmol.zip\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----- \n", "\n", "### Applet and Jmol/Jsmol examples in Jupyter notebook cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I was hoping to get it to work in a notebook and found the original source of this code by searching 'simple jmol page' and seeing the 3rd one listed was on sourcrforge and had text saying 'HTML5', clicking on it took me to http://jmol.sourceforge.net/ which listed a link to demos under 'Samples' that took me to [Demonstration page](http://jmol.sourceforge.net/demo/) that had a link to 'Very simple page, with source code and explanations' at http://jmol.sourceforge.net/demo/jssample0.html ,but it didn't seem to work even when I edited it like below to get javascript from other places (but see next cell for more about applets encoded in notebook cell attempt that lead me to try this again):\n", "**(NEXT CELL DOESN'T WORK, ONES BELOW DO!! But detailing process so if something breaks can more easily troubleshoot)**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%HTML\n", "\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the unedited (original source) version of that from http://jmol.sourceforge.net/demo/jssample0.html was:\n", "```javascript\n", "\n", "\n", "\n", "\n", "
\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So after that failure I showed following procedure and code in `trying Jmol applet in a Jupyter notebook.ipynb` that I could at least get Jsmol code to run inside jupyter's `%%HTML` magics. It had issues though that it took over the page and oddly didn't load what it was supposed to and when it did it was zoomed out invisible and couldn't be moved by mouse.\n", "\n", "So rexamined the code that I used `trying Jmol applet in a Jupyter notebook.ipynb` (from https://stackoverflow.com/a/44995828/8508004 ) in comparison to the simple code I was trying in the notebook **because I knew I got a white space** and because of effort with `trying Jmol applet in a Jupyter notebook.ipynb`, I sort of wondered if molecule was there but too zoomed out to see. Maybe the javascript set up that at least causes something to work could inform me and I could get the code pasted in working. Worth a shot. So I edited above again, this time tried following and it worked with the adpatations based on code from https://stackoverflow.com/a/44995828/8508004 that almost worked but took over page:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%HTML\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I don't honestly even know where it is getting caffeine molecule from because I don't see cafffein listed in `jsmol` directory, but very pleased to see **IT WORKS** and can spin caeffeine molecule with mouse in notebook cell. \n", "(Note that given https://chemapps.stolaf.edu/jmol/jsmol/jsmolgl.htm I think it means the load with dollar sign defaults to getting structure details from NCI and so this would mean this applet running in the cell can load structures from other places on web, too.)\n", "\n", "I'm noticing though that I cannot fully use console it seems. For example, I was trying to type in `set antialiasDisplay` and as I started typing with the `s` it kept triggering a notebook save step and not prinitng the `s` but prinint just `et`. Still major progress that this simple code works in the notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that WebGL can be used, too, based on https://chemapps.stolaf.edu/jmol/jsmol/jsmolgl.htm to get **better lighting**:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%HTML\n", "\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note WebGL doesn't render the background color.\n", "\n", "Can use `=` in front of PDB id to to fetch a structure from PDB/RCSB online as illustrated [here](https://chemapps.stolaf.edu/jmol/jsmol/jsmolgl.htm). Trying that with some other variations. (Do I need to clear output first or can multiple run on page?)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%HTML\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First try moving script location to be like source at https://chemapps.stolaf.edu/jmol/jsmol/jsmolgl.htm because I think script farther on left will be easier to edit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%HTML\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can WebGL be used with proteins, too?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%HTML\n", "\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Amazing jupyter-jsmol\n", "\n", "(I had a whole folder on my computer wondering how to do this and I had even contact Bob Hanson to floar the idea. I need to update my own documents in light of finding this.)\n", "\n", "Searching 'jsmol jupyter notebook' to hopefully find source of simple applet I had tried in notebook, I saw:\n", "\n", "https://pypi.org/project/jupyter-jsmol/\n", "\n", "Under Project Links on the right, is a link to the 'Homepage'\n", "which takes you to:\n", "https://github.com/fekad/jupyter-jsmol\n", "\n", "And it has a `launch binder` badge that launched where it seems to work!!! Fairly recent commits from 4 months ago when I found it January 2021." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#%pip install ipywidgets\n", "#%pip install jupyter_jsmol" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from jupyter_jsmol import JsmolView\n", "from ipywidgets import Layout, widgets, interact" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!curl -OL https://raw.githubusercontent.com/fekad/jupyter-jsmol/master/examples/data/c2h410.xyz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "view1 = JsmolView.from_file(\"c2h410.xyz\") \n", "# view1 = JsmolView.from_file(\"data/c2h410.xyz\", inline=False) \n", "view1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "view1.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "view2 = JsmolView.from_file(\"c2h410.xyz\", inline=True) \n", "view2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "view2.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "view5.close_all()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import time\n", "\n", "def executeSomething():\n", " #code here\n", " print ('.')\n", " time.sleep(480) #60 seconds times 8 minutes\n", "\n", "while True:\n", " executeSomething()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.8" } }, "nbformat": 4, "nbformat_minor": 4 }