blob: 76a14e411d110509e83d9159f11d83032a34e3bb [file]
= Python Language
:doctitle: Python
:shortname: python
:artifactid: camel-python
:description: Evaluates a Python expression
:since: 3.19
:supportlevel: Experimental
:tabs-sync-option:
*Since Camel {since}*
Camel allows https://www.jython.org/[Python] to be
used as an xref:manual::expression.adoc[Expression] or xref:manual::predicate.adoc[Predicate]
in Camel routes.
For example, you can use Python in a xref:manual::predicate.adoc[Predicate]
with the xref:eips:choice-eip.adoc[Content-Based Router] EIP.
== Python Options
// language options: START
include::partial$language-options.adoc[]
// language options: END
== Variables
[width="100%",cols="10%,10%,80%",options="header",]
|=======================================================================
|Variable |Type |Description
|*this* |Exchange |the Exchange is the root object
|exchange |Exchange |the Exchange
|camelContext |CamelContext |the CamelContext
|exchangeId |String |the exchange id
|message |Message |the message
|request |Message |the message (same as `message`)
|body |Object |the message body
|header |Map |the message headers (same as `headers`)
|headers |Map |the message headers
|exchangeProperty |Map |the exchange properties (same as `exchangeProperties`)
|exchangeProperties |Map |the exchange properties
|variable |Map |the exchange variables (same as `variables`)
|variables |Map |the exchange variables
|exception |Exception |the exception if the exchange failed (or the caught exception in an error handler), otherwise `None`
|context |CamelContext |*Deprecated* use `camelContext`
|properties |Map |*Deprecated* use `exchangeProperties`
|=======================================================================
The variables have the same names as in the xref:groovy-language.adoc[Groovy] language, except `in`, which is a
reserved word in Python (use `message` or `request`), and the deprecated `out` and `response`. The deprecated
`context` and `properties` still work.
The `variable` and `variables` bindings are the exchange-scoped variables (for example set with the
xref:eips:setVariable-eip.adoc[Set Variable] EIP). Global and route variables, such as `global:foo`,
are not in this map; use `exchange.getVariable('global:foo')` for those.
== Examples
A Python predicate in a xref:eips:choice-eip.adoc[Content-Based Router]:
[source,yaml]
----
- route:
from:
uri: direct:orders
steps:
- choice:
when:
- expression:
python:
expression: "headers.get('priority') == 'high'"
steps:
- to:
uri: direct:fast
otherwise:
steps:
- to:
uri: direct:normal
----
A Python expression that sets the message body from the body and a header:
[source,yaml]
----
- route:
from:
uri: direct:greet
steps:
- setBody:
expression:
python:
expression: "'Hello ' + str(body) + ', your priority is ' + str(headers.get('priority'))"
- to:
uri: log:greeting
----
== Dependencies
To use Python in your Camel routes, you need to add the dependency on
*camel-python* which implements the Python language.
If you use Maven, you could add the following to your `pom.xml`,
substituting the version number for the latest release.
[source,xml]
-------------------------------------
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-python</artifactId>
<version>x.x.x</version>
</dependency>
-------------------------------------