Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python-markdown new install fails to render {{var}}. Browser error 'TypeError: marked is not a function ' #1638

Open
johnjbarton opened this issue May 7, 2023 · 10 comments

Comments

@johnjbarton
Copy link

(New user here)
python-markdown seems like a killer feature... but rendering python silently fails for me.

I assume this is some config/install issue. I've not found any debug procedure or hints.

In the browser debugger I see 'TypeError: marked is not a function '. That seems bad ;-)

markdown does render otherwise, just the variable interpolation fails. The double braces and the var are missing:

Davidson Gemer electron velocity {{ dg_electron_velocity }} kg/sec and wavelength {{ dg_wavelength }}

becomes

Davidson Gemer electron velocity kg/sec and wavelength

@johnjbarton
Copy link
Author

Looks like the fail is here:
html = marked(ul['text/plain']);
in python-markdown/main.js.
The object ul['text/plain'] is the python value to be displayed.

So the 'marked is not a function' error is symptom, not a cause. The entry in ul has not been converted to something that markdown can handle.

@juhasch
Copy link
Member

juhasch commented May 7, 2023

This is due to changes in the notebook main code itself. The "marked" library is no longer included there.
It can be fixed, but it is a little bit tricky. In case you want to try:

  1. Download the marked.js file from https://cdnjs.com/libraries/marked and copy it to the python-markdown extension directory
  2. Replace 'components/marked/lib/marked',with nbextensions/python-markdown/marked.min',
  3. Replace all calls to marked(...) by marked.marked(...) calls in main.js of the extension.

@johnjbarton
Copy link
Author

johnjbarton commented May 7, 2023 via email

johnjbarton added a commit to johnjbarton/jupyter_contrib_nbextensions that referenced this issue May 7, 2023
In v6 jupyter notebook components/marked/lib/marked no longer exists. The extension silently falls over. 
Hack around puts local copy of marked.min from cdnjs.com into the python-markdown directory.
See Issue ipython-contrib#1638
@johnjbarton
Copy link
Author

Ok I cloned the repo and edited per your instructions.
I used https://github.com/ipython-contrib/jupyter_contrib_nbextensions/blob/master/CONTRIBUTING.md#setup-development
but when I run jupyter notebook I still get the original main.js, rather than my modified one.

Where might I ask about dev setup issues?

Is committing marked.min to python-markdown/ in the plan for v6+?

Maybe the two different marked libs could both be required and have a runtime test/fallback?

@johnjbarton
Copy link
Author

I accidently noticed that on the nbextensions_configurator tab there was an info panel: "This nbextension's require url (python-markdown/main) is referenced by two different yaml files on the server." The server log confirms that this duplicate issue.

I don't recognize either path however, eg
[W 17:18:10.571 NotebookApp] [jupyter_nbextensions_configurator] nbextension 'python-markdown/main' has duplicate listings in both 'C:\Users\jjb\AppData\Local\Programs\Python\Python311\share\jupyter\nbextensions\python-markdown\python-markdown.yaml' and 'C:\Users\jjb\AppData\Roaming\jupyter\nbextensions\python-markdown\python-markdown.yaml'

There must be some server config that points now to two roots for nbextensions.

@johnjbarton
Copy link
Author

Using this hint
I ran
jupyter contrib nbextensions uninstall --user
and the duplicates are gone and the Python variable interpolation works.

@johnjbarton
Copy link
Author

Unfortunately I succeeded by dumb luck. I made more edits and these do not appear in the app. I tried various uninstall/install commands but no dice.

It seems as if the app runs off stuff in AppData rather than my git repo directly. Some command must copy files into the AppData.

The command
jupyter contrib nbextension install --user
did the trick. IDK why.

johnjbarton added a commit to johnjbarton/jupyter_contrib_nbextensions that referenced this issue May 8, 2023
…h fallback for v<6

In v6 jupyter notebook components/marked/lib/marked no longer exists. The extension silently falls over.
Hack around puts local copy of marked.min from cdnjs.com into the python-markdown directory.

require 'marked' on two paths,
first expecting marked.min.j in the same dir,
second expecting marked in the v6 compenents/ location.

Insert ERROR message in user page if neither is found.
@johnjbarton
Copy link
Author

My fix is in #1639

johnjbarton added a commit to johnjbarton/jupyter_contrib_nbextensions that referenced this issue May 9, 2023
…d.js

In v6 jupyter notebook components/marked/lib/marked no longer exists. The extension silently falls over.

Add  https://cdnjs.com/libraries/marked for local loading into the python-markdown directory.

Change required path and calls marked -> marked.marked.

Add 6.x to yaml compat setting
@bradenmitchell
Copy link

Any update on this? I am having the same issues outputting just the example'a = 1.23' '{{a}}' in the extensions documentation.

@phlummox
Copy link

phlummox commented Aug 23, 2023

@bradenmitchell I don't know if it is useful, but I can confirm that @johnjbarton's patches work, at least for me. You should be able to use them, instead of the default Jupyter notebook extensions, by running:

pip install 'git+https://github.com/johnjbarton/jupyter_contrib_nbextensions.git@84aeb3b0c9880d47c562d80d5155fb87caaecac2'

(Probably best to do so in a venv.)

I've also got a small docker-compose example demonstrating them - details below.

docker-compose example of johnjbarton's patches in use

Dockerfile:

FROM jupyter/base-notebook:notebook-6.5.4

USER root

RUN \
  apt-get update && \
  apt-get --no-install-recommends install -yq \
      git

USER jovyan

# (not sure if ipykernel need to be upgraded, but some posts
# on this bug thread report it might)

RUN \
  pip install --no-cache-dir --user --upgrade \
    ipykernel \
'git+https://github.com/johnjbarton/jupyter_contrib_nbextensions.git@84aeb3b0c9880d47c562d80d5155fb87caaecac2'

# also, jupyter_lsp seems not to work and throws errors on server
# startup, so we disable it.

RUN \
      export PATH=$HOME/.local/bin:$PATH \
  &&  jupyter contrib nbextension install --user \
  &&  jupyter nbclassic-extension enable python-markdown/main --user \
  &&  jupyter serverextension disable jupyter_lsp \
  &&  fix-permissions "${CONDA_DIR}" \
  &&  fix-permissions "/home/${NB_USER}"

RUN \
  echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc  

docker-compose.yml:

version: '3'
services:
  jupyter:
    build: .

    ports:
      - "8888:8888"
    volumes:
      - ".:/home/jovyan/work"
    environment:
      - "DOCKER_STACKS_JUPYTER_CMD=notebook"

mynotebook.ipynb:

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "30e30612",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = \"some value\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d20c6c3a",
   "metadata": {
    "variables": {
     "repr(x)": "&quot;&#39;some value&#39;&quot;"
    }
   },
   "source": [
    "The value of x is {{repr(x)}}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22f26b7d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

To use this example, run docker-compose up.

@juhasch or other maintainers - are the patches suggested by johnjbarton likely to be incorporated into the repo, thus fixing this bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants