Python API that handles syntax errors in PlantUML code

0 votes
asked Mar 25, 2022 in Question / help by Fuhrmanator (1,700 points)
edited Mar 25, 2022 by Fuhrmanator

We're trying to use PlantUML in jupyter notebooks / colab.research.google.com notebooks. There are several APIs for PlantUML and Python. So far I have tried:

  • plantuml
  • plantweb
  • iplantuml

But sadly none of them have sufficient error handling (at least from the usage of jupyter/colab) to allow displaying the syntax errors in a cell. See this issue for more details: https://github.com/jbn/IPlantUML/issues/22

This is frustrating for students trying to learn to use this in courses, since the APIs crash when there are errors in the PlantUML code.

Does anyone know of a Python API that has a means to display gracefully the errors that come back concerning the PlantUML syntax (rather than crashing)?

1 Answer

0 votes
answered Mar 29, 2022 by Fuhrmanator (1,700 points)

We worked around this by installing the patched version of `plantuml`:

  1. !pip3 install --upgrade git+https://github.com/jean/python-plantuml.git#egg=plantuml

Here's the %%uml script for colab that catches and displays the error:

from IPython.core.magic import (Magics, magics_class, cell_magic)
from IPython.core.magics.display import DisplayMagics
from IPython.display import display, Image
from plantuml import PlantUML, PlantUMLHTTPError

# On utilise une classe magics au cas où on veut garder l'état plus tard
@magics_class
class UMLMagics(DisplayMagics):
  urlPlantUML = 'http://www.plantuml.com/plantuml/img/'

  def __init__(self, shell):
    super(UMLMagics, self).__init__(shell=shell)
    self.server = PlantUML(url=UMLMagics.urlPlantUML)

  @cell_magic
  def uml(self, line='', cell=None):
    try:
      img = Image(self.server.processes(cell))
      display(img)
    except PlantUMLHTTPError as e:
      img = Image(e.content)
      display(img)

try:
  ip = get_ipython()
  magics = UMLMagics(ip)
  ip.register_magics(magics)

except NameError:
  print("iPython non disponible.")
  pass
...