### What is this PR for? Conda interpreter that manages conda environment for PythonInterpreter ### What type of PR is it? Feature ### Todos * [x] - Basic impl * [x] - update doc ### What is the Jira issue? https://issues.apache.org/jira/browse/ZEPPELIN-1671 ### How should this be tested? Recreate(or create new) your python interpreter setting in gui. List all conda env ``` %python.conda ``` Activate env ``` %python.conda activate [name] ``` Deactivate env ``` %python.conda deactivate ``` ### Screenshots (if appropriate)  ### Questions: * Does the licenses files need update? no * Is there breaking changes for older versions? no * Does this needs documentation? yes Author: Lee moon soo <moon@apache.org> Closes #1645 from Leemoonsoo/conda and squashes the following commits:4842b0a[Lee moon soo] Add usage in docd979c6a[Lee moon soo] Add unittestb889443[Lee moon soo] add usage template9ae553b[Lee moon soo] Format output and add usage command171cbeb[Lee moon soo] make sure single char interpreter name can be parsed6b9525f[Lee moon soo] Fix unittest1223796[Lee moon soo] Remove unnecessary log394cf8c[Lee moon soo] Conda interpreter implementation
6.5 KiB
| layout | title | description | group |
|---|---|---|---|
| page | Python 2 & 3 Interpreter for Apache Zeppelin | Python is a programming language that lets you work quickly and integrate systems more effectively. | interpreter |
{% include JB/setup %}
Python 2 & 3 Interpreter for Apache Zeppelin
Configuration
| Property | Default | Description |
|---|---|---|
| zeppelin.python | python | Path of the already installed Python binary (could be python2 or python3). If python is not in your $PATH you can set the absolute directory (example : /usr/bin/python) |
| zeppelin.python.maxResult | 1000 | Max number of dataframe rows to display. |
Enabling Python Interpreter
In a notebook, to enable the Python interpreter, click on the Gear icon and select Python
Using the Python Interpreter
In a paragraph, use %python to select the Python interpreter and then input all commands.
The interpreter can only work if you already have python installed (the interpreter doesn't bring it own python binaries).
To access the help, type help()
Python modules
The interpreter can use all modules already installed (with pip, easy_install...)
Conda
Conda is an package management system and environment management system for python.
%python.conda interpreter lets you change between environments.
Usage
List your environments
%python.conda
Activate an environment
%python.conda activate [ENVIRONMENT_NAME]
Deactivate
%python.conda deactivate
Using Zeppelin Dynamic Forms
You can leverage Zeppelin Dynamic Form inside your Python code.
Zeppelin Dynamic Form can only be used if py4j Python library is installed in your system. If not, you can install it with pip install py4j.
Example :
%python
### Input form
print (z.input("f1","defaultValue"))
### Select form
print (z.select("f1",[("o1","1"),("o2","2")],"2"))
### Checkbox form
print("".join(z.checkbox("f3", [("o1","1"), ("o2","2")],["1"])))
Matplotlib integration
The python interpreter can display matplotlib figures inline automatically using the pyplot module:
%python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
This is the recommended method for using matplotlib from within a Zeppelin notebook. The output of this command will by default be converted to HTML by implicitly making use of the %html magic. Additional configuration can be achieved using the builtin z.configure_mpl() method. For example,
z.configure_mpl(width=400, height=300, fmt='svg')
plt.plot([1, 2, 3])
Will produce a 400x300 image in SVG format, which by default are normally 600x400 and PNG respectively. In the future, another option called angular can be used to make it possible to update a plot produced from one paragraph directly from another (the output will be %angular instead of %html). However, this feature is already available in the pyspark interpreter. More details can be found in the included "Zeppelin Tutorial: Python - matplotlib basic" tutorial notebook.
If Zeppelin cannot find the matplotlib backend files (which should usually be found in $ZEPPELIN_HOME/interpreter/lib/python) in your PYTHONPATH, then the backend will automatically be set to agg, and the (otherwise deprecated) instructions below can be used for more limited inline plotting.
If you are unable to load the inline backend, use z.show(plt):
%python
import matplotlib.pyplot as plt
plt.figure()
(.. ..)
z.show(plt)
plt.close()
The z.show() function can take optional parameters to adapt graph dimensions (width and height) as well as output format (png or optionally svg).
%python
z.show(plt, width='50px')
z.show(plt, height='150px', fmt='svg')
Pandas integration
Apache Zeppelin Table Display System provides built-in data visualization capabilities. Python interpreter leverages it to visualize Pandas DataFrames though similar z.show() API, same as with Matplotlib integration.
Example:
import pandas as pd
rates = pd.read_csv("bank.csv", sep=";")
z.show(rates)
SQL over Pandas DataFrames
There is a convenience %python.sql interpreter that matches Apache Spark experience in Zeppelin and enables usage of SQL language to query Pandas DataFrames and visualization of results though built-in Table Display System.
Pre-requests
- Pandas
pip install pandas - PandaSQL
pip install -U pandasql
In case default binded interpreter is Python (first in the interpreter list, under the Gear Icon), you can just use it as %sql i.e
- first paragraph
import pandas as pd
rates = pd.read_csv("bank.csv", sep=";")
- next paragraph
%sql
SELECT * FROM rates WHERE age < 40
Otherwise it can be referred to as %python.sql
Technical description
For in-depth technical details on current implementation please refer to python/README.md.
Some features not yet implemented in the Python Interpreter
- Interrupt a paragraph execution (
cancel()method) is currently only supported in Linux and MacOs. If interpreter runs in another operating system (for instance MS Windows) , interrupt a paragraph will close the whole interpreter. A JIRA ticket (ZEPPELIN-893) is opened to implement this feature in a next release of the interpreter. - Progression bar in webUI (
getProgress()method) is currently not implemented. - Code-completion is currently not implemented.