.. _userguide_concepts:

Defining Key Concepts
=====================

.. _userguide_glossary:

Glossary
--------

These are the most important concepts and terms that you will encounter
throughout Bokeh's documentation:

.. glossary::

    Annotation
        Visual aids that make reading the plot easier. This includes titles,
        legends, labels, or bands, for example. See :ref:`userguide_annotations`
        in the :ref:`userguide` for more information and examples.

    Application
        A Bokeh application is a recipe for generating Bokeh
        :term:`documents<Document>`. Typically, this is Python code run by a
        Bokeh :term:`server<Server>` whenever a new sessions is created.

    BokehJS
        The JavaScript client library that actually renders the visuals and
        handles the UI interactions for Bokeh :term:`plots<Plot>` and
        :term:`widgets<Widget>` in the browser. In most cases, Bokeh handles all
        interactions with BokehJS automatically ("We write the JavaScript, so you
        don't have to"). For more details, see the :ref:`devguide_bokehjs`
        chapter of the :ref:`developers guide<devguide>`.

    Document
        An organizing data structure for Bokeh
        :term:`applications<Application>`. Documents contain all the Bokeh
        :term:`models<Model>` and data needed to render an interactive
        visualization or application in the browser.

    Embedding
        Various methods that help with including Bokeh :term:`plots<Plot>` and
        :term:`widgets<Widget>` in web apps, web pages, or Jupyter notebooks.
        See :ref:`userguide_embed` in the :ref:`userguide` for more details.

    Glyph
        API objects that draw vectorized graphics to represent data. Glyphs are
        the basic visual building blocks of Bokeh :term:`plots<Plot>`. This
        includes elements such as lines, rectangles, squares, wedges, or the
        circles of a scatter plot. The :ref:`userguide_interfaces_plotting`
        interface provides a convenient way to create plots centered around
        glyphs. See :ref:`userguide_plotting` in the :ref:`userguide` for more
        information.

    Layout
        A collection of Bokeh objects. This can be several :term:`plots<Plot>`
        and :term:`widgets<Widget>`, arranged in nested rows and columns. See
        :ref:`userguide_layout` in the :ref:`userguide` for more information and
        examples.

    Model
        The lowest-level objects that Bokeh visualizations consist of. Bokeh's
        models are part of the :ref:`userguide_interfaces_models` interface.
        Most users will not use this level of interface to assemble plots
        directly. However, ultimately all Bokeh plots consist of collections of
        models. It is helpful to understand them enough to configure their
        attributes and properties. See :ref:`userguide_styling` in the
        :ref:`userguide` for more information.

    Plot
        Containers that hold all the various objects (such as
        :term:`renderers<Renderer>`, :term:`glyphs<Glyph>`, or
        :term:`annotations<Annotation>`) of a visualization. The
        :ref:`userguide_interfaces_plotting` interface provides the
        :func:`~bokeh.plotting.figure` function to help with assembling all the
        necessary objects.

    Renderer
        General term for any method or function that draws elements of the plot.
        Examples of elements that are generated by renderers are
        :term:`glyphs<Glyph>` or :term:`annotations<Annotation>`.

    Server
        The Bokeh server is an optional component. You can use the Bokeh server
        to share and publish Bokeh plots and apps, to handle streaming of large
        data sets, or to enable complex user interactions based on
        :term:`widgets<Widget>` and selections. See :ref:`userguide_server` in
        the :ref:`userguide` for more information and examples.

    Widget
        User interface elements that are not directly part of a Bokeh
        :term:`plot<Plot>`, such as sliders, drop-down menus, or buttons. You
        can use events and data from widgets in your Python code, or you can
        use input from widgets to update you Bokeh plot itself. You can use
        widgets in standalone :term:`applications<Application>` or with the
        Bokeh :term:`server<Server>`. For examples and information, see
        :ref:`userguide_interaction` in the :ref:`userguide`.

.. _userguide_output_methods:

Output methods
--------------

Bokeh offers a variety of ways to produce interactive output. The following
two functions are the most common:

:func:`~bokeh.io.output_file`
    Generate simple standalone HTML documents for Bokeh visualizations.

:func:`~bokeh.io.output_notebook`
    Display Bokeh visualizations in Jupyter/Zeppelin notebooks.

These output functions are usually used together with :func:`~bokeh.io.show`
or :func:`~bokeh.io.save`. Here's an example:

.. code-block:: python

    from bokeh.plotting import figure, output_file, show

    output_file("output.html")

    p = figure()
    p.line(x=[1, 2, 3], y=[4,6,2])

    show(p)

This script generates an HTML file called ``output.html`` that contains a line
plot. You can execute it with ``python foo.py``, where ``foo.py`` is the name
of the script.

These functions are often useful in interactive settings or for creating
standalone Bokeh documents to serve from backend web applications.

Another way to generate various kinds of output is by using Bokeh's powerful
:ref:`command line tool <userguide_cli>`.

.. _userguide_interfaces:

Interfaces
----------

Bokeh provides a simple and intuitive interface for users like data scientists
and domain experts who do not wish to be distracted by complex details of the
software. At the same time, Bokeh also caters to people such as application
developers and software engineers who may want more control or access to more
sophisticated features.

Because of this, Bokeh takes a layered approach and offers different programming
interfaces appropriate to different users.

This section provides an overview of the two interfaces that you can use: the
primary interface :ref:`bokeh.plotting <userguide_interfaces_plotting>` and
the low-level :ref:`bokeh.models<userguide_interfaces_models>` interface.

If you'd prefer to jump right into basic plotting, go to
:ref:`userguide_plotting`. For a simple step-by-step guide to creating
visualizations with Bokeh, see the :ref:`first steps guides <first_steps>`.

.. _userguide_interfaces_plotting:

The *bokeh.plotting* interface
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

|bokeh.plotting| is Bokeh's **primary** interface. This general-purpose
interface is similar to plotting interfaces of libraries such as Matplotlib_
or Matlab_.

The *bokeh.plotting* interface lets you focus on relating glyphs to data. It
automatically assembles plots with default elements such as axes, grids, and
tools for you.

The |figure| function is at the core of the |bokeh.plotting| interface. This
function creates a |Figure| model that includes methods for adding different
kinds of glyphs to a plot. This function also takes care of composing the
various elements of your visualization, such as axes, grids, and tools.

Below is an example of |bokeh.plotting|, along with the resulting
plot:

.. bokeh-plot:: docs/user_guide/examples/concepts_plotting.py
    :source-position: above

Calling the |figure| function is all it takes to create a basic plot object. To
add data renderers to your plot object, call a glyph method such as
|Figure.circle|. You don't have to worry about axes and grids (although you can
configure them if you want to), and you only need to list the tools you want to
add. To display your visualization in a browser, in most cases, all you need to
do is call the output function :func:`~bokeh.io.show`.

With the *bokeh.plotting* interface, you have many more possibilities to
create an customize your visualization. For example:

* saving the plot to an HTML file instead of showing it
* styling and removing axes, grids, annotations, and interactive elements
* adding more data renderers
* arranging multiple plots and widgets into layouts

The :ref:`userguide_plotting` section of this :ref:`userguide` will walk you
through many more examples and common use cases for the |bokeh.plotting|
interface.

For an easy to follow guide to building your first visualizations with Bokeh,
see the :ref:`first steps guides <first_steps>`.

.. _userguide_interfaces_models:

The *bokeh.models* interface
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With Bokeh's **low-level** |bokeh.models| interface, you have complete control
over how Bokeh creates all elements of your visualization. However, Bokeh's
low-level interface doesn't help you assemble the various elements in meaningful
or correct ways. It is entirely up to you to put them together.

Therefore, unless you have special applications that require finer control,
you will probably want to use the *bokeh.plotting* interface
:ref:`described above <userguide_interfaces_plotting>`.

To be able to use the *bokeh.models* interface, you need to understand the
basic principle by which Bokeh enables you to generate interactive,
browser-based visualizations. Behind the scenes, Bokeh consists of two
libraries:

* BokehJS, the JavaScript library
    BokehJS runs in the browser. This library handles rendering and user
    interactions. It takes a collection of declarative JSON objects as its input
    and uses them as instructions on how to handle the various aspects of your
    visualization in a browser. For example:

    * plots and widgets
    * layouts and arrangements
    * tools and renderers
    * plot axes

    In the browser, BokehJS converts these JSON objects into BokehJS models and
    renders them according to corresponding BokehJS views.

* Bokeh, the Python library
    The Python library generates the JSON objects that BokehJS uses to render
    your visualization in a browser.

    At its lowest level, the Python library uses a set of ``model`` classes that
    exactly mirror the set of models that BokehJS creates in a browser.

    These Python ``model`` classes are able to validate their content and
    attributes and serialize themselves to JSON. Most of the models are very
    simple and usually consist of only a few property attributes and no methods.
    You can configure the attributes of those models either by setting them when
    creating a model or later by setting attribute values on the model object.

You can access all low-level ``model`` objects through Bokeh's |bokeh.models|
interface.

For example, to create and configure a |Rect| glyph object:

.. code-block:: python

    # configure attributes when initializing a model object
    glyph = Rect(x="x", y="y2", w=10, h=20, line_color=None)

    # assign values to attributes to an existing model object
    glyph.fill_alpha = 0.5
    glyph.fill_color = "navy"

You can generally configure all Bokeh models this way. Since all Bokeh
interfaces ultimately produce collections of Bokeh models, this lets you
style and configure plots and widgets the same way regardless of the
interface.

For more information on Bokeh models, consult the :ref:`refguide`.

.. note::

   The Python library allows for binding with other languages that can produce
   appropriate JSON output. For more details and available bindings, see
   :ref:`devguide_bindings`.


.. _Matlab: http://www.mathworks.com/products/matlab/
.. _Matplotlib: http://matplotlib.org

.. |bokeh.models|   replace:: :ref:`bokeh.models <bokeh.models>`
.. |bokeh.plotting| replace:: :ref:`bokeh.plotting <bokeh.plotting>`
.. |bokeh.io|       replace:: :ref:`bokeh.io <bokeh.io>`

.. |Plot| replace:: :class:`~bokeh.models.plots.Plot`

.. |Rect| replace:: :class:`~bokeh.models.glyphs.Rect`

.. |output_file|     replace:: :func:`~bokeh.io.output_file`
.. |output_notebook| replace:: :func:`~bokeh.io.output_notebook`
.. |save|            replace:: :func:`~bokeh.io.save`
.. |show|            replace:: :func:`~bokeh.io.show`

.. |figure|          replace:: :func:`~bokeh.plotting.figure`
.. |Figure|          replace:: :class:`~bokeh.plotting.Figure`
.. |Figure.circle|   replace:: :func:`Figure.circle <bokeh.plotting.Figure.circle>`
