Difference between revisions of "Python shell"

From GnuCash
Jump to: navigation, search
(Activation: debug shows location of the file)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The python shell provides a gtk window with a python shell.
+
[[Category:Python]]
 +
The python shell provides a gtk window with a python shell. The shell is based on the work of Nicolas Rougier from 2008. There have been some changes to python and ipython api since then that have not yet been fully integrated.
  
= Installing and Activation =
+
= Installation and Activation =
  
== Installing ==
+
== Installation ==
  
 
Python shell comes with the python bindings. [[Python Bindings#Setting things up|Install those]].
 
Python shell comes with the python bindings. [[Python Bindings#Setting things up|Install those]].
Line 11: Line 12:
 
The source of the file init.py needs to modified. When compiling gnucash from source it can be done beforehand. If installing from packages the installed file needs to be changed. It may be at different places depending on your distribution / the way it has been installed - something like "local/share/gnucash/python/init.py". "which gnucash" to locate the gnucash binary may be a start to look for it. Enabling [[#extra debug output|extra debug output]] will output the location of the file.
 
The source of the file init.py needs to modified. When compiling gnucash from source it can be done beforehand. If installing from packages the installed file needs to be changed. It may be at different places depending on your distribution / the way it has been installed - something like "local/share/gnucash/python/init.py". "which gnucash" to locate the gnucash binary may be a start to look for it. Enabling [[#extra debug output|extra debug output]] will output the location of the file.
  
'''init.py'''
+
'''init.py''' <syntaxhighlight lang="py">
 
+
# Change this to "if True:" to switch on a python console at gnucash
# Change this to "if True:" to switch on a python console at gnucash
+
# startup:
# startup:
+
# shelltype can either be "python" or "ipython" (the latter is not yet fully functional)
# shelltype can either be "python" or "ipython" (the latter is not yet fully functional)
+
if False:
if False:
+
</syntaxhighlight>
 
+
Change to: <syntaxhighlight lang="py">
Change to  
+
if True:
 
+
</syntaxhighlight>
if True:
 
  
 
=== extra debug output ===
 
=== extra debug output ===
  
The python module will print out some additional information when gnucash is started with extra debug output:
+
The python module will print out some additional information when gnucash is started with extra debug output: <syntaxhighlight lang="sh">
 +
gnucash --debug --extra
 +
</syntaxhighlight>
  
gnucash --debug --extra
+
(see [[Tracefile#Trace_File_Adjustments]])
  
(see [[Tracefile#Trace_File_Adjustments]])
+
One important information here is the location of the file.
  
 
== Location ==
 
== Location ==
Line 37: Line 39:
  
 
The shell provides a python and an ipython interface. Just now only the python shell is working. Ipython had some major API-changes in Version 5.
 
The shell provides a python and an ipython interface. Just now only the python shell is working. Ipython had some major API-changes in Version 5.
 +
 +
== Usage examples ==
 +
 +
The [[Python Bindings]] can be accessed using the gnucash module: <syntaxhighlight lang="py">
 +
import gnucash
 +
</syntaxhighlight>
 +
 +
To get information about the current book and root_account there are some helpful methods in _sw_app_utils: <syntaxhighlight lang="py">
 +
from gnucash import _sw_app_utils
 +
session_instance = _sw_app_utils.gnc_get_current_session()
 +
# the next line doesn't work at the moment as Session wrapper has no instance argument (requested in https://github.com/Gnucash/gnucash/pull/678)
 +
# session = gnucash.Session(instance=session_instance)
 +
book_instance = _sw_app_utils.gnc_get_current_book()
 +
book = gnucash.Book(instance = book_instance)
 +
root_account_instance = _sw_app_utils.gnc_get_current_root_account()
 +
root_account = gnucash.Account(instance = root_account_instance)
 +
</syntaxhighlight>
 +
 +
To get more information have a look at [{{BuildURL}}/docs/MAINT/group__python__bindings__examples.html group  python  bindings  examples] and in [[Python Bindings]].
  
 
= Warning =
 
= Warning =
  
 
Be careful - if you use the [[Python Bindings|python bindings]] in the shell to work on gnucash financial data: gnucash is not designed to have multiple instances changing the data at the same time, reading should be secure.
 
Be careful - if you use the [[Python Bindings|python bindings]] in the shell to work on gnucash financial data: gnucash is not designed to have multiple instances changing the data at the same time, reading should be secure.
 +
 +
= Background =
 +
 +
The shell is based on the work of Nicolas Rougier from 2008. IPython api has changed a lot since then. The changes have not been reflected in gnucash python shell.
 +
 +
== Links ==
 +
 +
Looking for other projects including (i)python shells for possible fixes of ipython shell:
 +
* https://stackoverflow.com/questions/16639199/embedding-an-ipython-shell-in-a-gtk-program#48054041
 +
* https://gitlab.gnome.org/GNOME/accerciser/-/blob/master/plugins/ipython_view.py
 +
* http://code.ascend4.org/ascend/trunk/pygtk/ipython_view.py?view=markup
 +
* https://github.com/ipython/ipython/wiki/Trash:-Old-Embedding-GTK
 +
* https://dev.gajim.org/gajim/gajim/commit/c3eba4037e902280436fe5afd8df22e1289e1f33

Latest revision as of 02:29, 22 March 2022

The python shell provides a gtk window with a python shell. The shell is based on the work of Nicolas Rougier from 2008. There have been some changes to python and ipython api since then that have not yet been fully integrated.

Installation and Activation

Installation

Python shell comes with the python bindings. Install those.

Activation

The source of the file init.py needs to modified. When compiling gnucash from source it can be done beforehand. If installing from packages the installed file needs to be changed. It may be at different places depending on your distribution / the way it has been installed - something like "local/share/gnucash/python/init.py". "which gnucash" to locate the gnucash binary may be a start to look for it. Enabling extra debug output will output the location of the file.

init.py
# Change this to "if True:" to switch on a python console at gnucash
# startup:
# shelltype can either be "python" or "ipython" (the latter is not yet fully functional)
if False:
Change to:
if True:

extra debug output

The python module will print out some additional information when gnucash is started with extra debug output:
gnucash --debug --extra

(see Tracefile#Trace_File_Adjustments)

One important information here is the location of the file.

Location

In the source tree the shell is at gnucash/python (while the python bindings are at bindings/python).

Configuration

The shell provides a python and an ipython interface. Just now only the python shell is working. Ipython had some major API-changes in Version 5.

Usage examples

The Python Bindings can be accessed using the gnucash module:
import gnucash
To get information about the current book and root_account there are some helpful methods in _sw_app_utils:
from gnucash import _sw_app_utils
session_instance = _sw_app_utils.gnc_get_current_session()
# the next line doesn't work at the moment as Session wrapper has no instance argument (requested in https://github.com/Gnucash/gnucash/pull/678)
# session = gnucash.Session(instance=session_instance)
book_instance = _sw_app_utils.gnc_get_current_book()
book = gnucash.Book(instance = book_instance)
root_account_instance = _sw_app_utils.gnc_get_current_root_account()
root_account = gnucash.Account(instance = root_account_instance)

To get more information have a look at group python bindings examples and in Python Bindings.

Warning

Be careful - if you use the python bindings in the shell to work on gnucash financial data: gnucash is not designed to have multiple instances changing the data at the same time, reading should be secure.

Background

The shell is based on the work of Nicolas Rougier from 2008. IPython api has changed a lot since then. The changes have not been reflected in gnucash python shell.

Links

Looking for other projects including (i)python shells for possible fixes of ipython shell: