setup.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, mapset):
  29. """
  30. This function initiates a GRASS session and sets GRASS
  31. environment variables.
  32. Inputs:
  33. path - path to grass databases
  34. location - name of GRASS location
  35. mapset - name of mapset within location
  36. """
  37. # Create a GRASS GIS session.
  38. gsetup.init(os.environ["GISBASE"], path, location, mapset)
  39. # Set GRASS env. variables
  40. _set_notebook_defaults()
  41. def display_settings(font="sans", driver="cairo"):
  42. """
  43. This function sets the display settings for a GRASS session
  44. in Jupyter Notebooks.
  45. Example Usage: display_settings(font="sans", driver="cairo")
  46. Inputs:
  47. font - specifies the font as either the name of a font from
  48. $GISBASE/etc/fontcap (or alternative fontcap file specified by
  49. GRASS_FONT_CAP), or alternatively the full path to a FreeType
  50. font file.
  51. driver - tell teh display library which driver to use
  52. Possible values: "cairo", "png", "ps", "html"
  53. """
  54. # Set display font
  55. os.environ["GRASS_FONT"] = font
  56. # Set display modeules to render to a file (named map.png by
  57. # default).
  58. os.environ["GRASS_RENDER_IMMEDIATE"] = driver
  59. os.environ["GRASS_RENDER_FILE_READ"] = "TRUE"