setup.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # MODULE: grass.jupyter.setup
  2. #
  3. # AUTHOR(S): Caitlin Haedrich <caitlin DOT haedrich AT gmail>
  4. #
  5. # PURPOSE: This module contains functions for launching a GRASS session
  6. # in Jupyter Notebooks
  7. #
  8. # COPYRIGHT: (C) 2021 Caitlin Haedrich, and by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. import os
  14. import grass.script as gs
  15. import grass.script.setup as gsetup
  16. def _set_notebook_defaults():
  17. """
  18. This function sets several GRASS environment variables that are
  19. important for GRASS to run smoothly in Jupyter.
  20. It also allows GRASS to overwrite existing maps of the same name.
  21. """
  22. # We want functions to raise exceptions and see standard output of
  23. # the modules in the notebook.
  24. gs.set_raise_on_error(True)
  25. gs.set_capture_stderr(True)
  26. # Allow overwrite of existing maps
  27. os.environ["GRASS_OVERWRITE"] = "1"
  28. def init(path, location=None, mapset=None, grass_path=None):
  29. """
  30. This function initiates a GRASS session and sets GRASS
  31. environment variables.
  32. :param str path: path to grass databases
  33. :param str location: name of GRASS location
  34. :param str mapset: name of mapset within location
  35. """
  36. # Create a GRASS GIS session.
  37. gsetup.init(path, location=location, mapset=mapset, grass_path=grass_path)
  38. # Set GRASS env. variables
  39. _set_notebook_defaults()