Changes

Jump to: navigation, search

Addon:Python Shell Gramplet

1,086 bytes added, 14:01, 4 January 2018
m
minor typo on my last edit
[[Category:Plugins]][[Category:Developers/General]]{{Third-party plugin}}
[[ImageFile:PythongadgetPythonGramplet-interactive-shell-example-50.png|thumb|right|200px450px|Fig. 4.8 Python ShellGramplet - Interactive shell example output]]
The Python gramplet Gramplet brings up a an interactive Python Shell for interpreting python expressions.
==Options==
You can type most any (single line) Python expression.
In addition, the environment has been populated with some useful variables, including *'''self''' - (this Python gramplet), *'''Date'''- a date object constructor, that can be used for date arithmetic.*'''uistatedb''' and - Interact with the Database*'''dbstate'''- Information about the Database*'''gc''' - the Python "garbage collector" preloaded with the same flags as the Debug Tool.*'''uistate''' - Interact with the GUI When the Python Gramplet is detached you can select the {{man button|Help}} button to see this page.
Note that the name '''Date''' will be translated into your language, if a translations is available.==Examples==
The '''Date''' entry is a date object constructor, and can be used for date arithmetic. For example, you might be interested in questions like:
How old was someone on Sept 3, 1955 who was born on June 7, 1922:
> Date(1955, 9, 3) - Date(1922, 5, 7) (33years, 3, 27)months
(About 33 years, 3 months, and 27 days). When did they turn 21 years old?
> Date(1922, 5, 7) + 21
1980-01-26
You can also use the same formats as you use in data entry:  > Date("Jan 15, 1962") 1962-15-01 > Date("15 Jan, 1962") 1962-15-01 > Date("1962-15-01") 1962-15-01 There are two ways to use different calendars:  > Date("Jan 15, 1532 (Julian)") 1532-15-01 (Julian) > Date(1671, 12, 31, calendar="julian") 1671-12-31 and a method to convert one date into another calendar, which returns a new date object:
> Date(1703, 6, 1).to_calendar("hebrew")
=== Developers ===
Another use for this Gramplet is for debugging. This gramplet makes a nice interface to the running GRAMPS Gramps system. You can inspect, and alter the system by entering Python commands. As a simple example, you can:
> self.clear_text() # clear the text in this window
> gc.get_referents(self)
[...]
> gc.get_referersget_referrers(self)
[...]
{{stub}}You can use the Python Shell to interact with people from your database and test GRAMPS Gramps functions:
{| border="1" style="border-collapse:collapse"! Gramps 3.x| <pre> > person = dbstate.db.get_person_from_gramps_id("I01284")
> from Utils import probably_alive
> probably_alive(pperson, dbstate.db, Date(1776, 7, 4))</pre>|-! Gramps 4.x| <pre> > person = db.get_person_from_gramps_id("I01284"July 4) > from gen.utils.alive import probably_alive > probably_alive(person, db, Date(1776", 7, 4))</pre>|}
You can also interact with the GUI:
This following returns the Gtk Frame of the first Gramplet in the first column.
 
{| border="1" style="border-collapse:collapse"
! Gramps 3.x
| <pre>
> uistate.viewmanager.pages
[<DataViews.GrampletView.GrampletView instance at 0xa0bd0ac>,
<DataViews.GrampletView.GrampletView instance at 0xa0bd0ac>
> uistate.viewmanager.pages[0].columns[0].get_children()[0].get_children()[0]
</pre>
|-
! Gramps 4.x
| <pre>
 
</pre>
|}
 
This following returns a reference to a loaded Gramplet object on the Event view - useful when developing Gramplets. Note that the Events view must be navigated first, as main views are lazy loading in Gramps 4.
 
{| border="1" style="border-collapse:collapse"
! Gramps 4.x
| <pre>
> evtview = uistate.viewmanager.pages[uistate.viewmanager.page_lookup.get((5,0))]
> # list all TabGramplet objects loaded for Events view
> [child.get_title() for child in evtview.bottombar.get_children()]
['Gallery', 'Citations', 'Notes', 'Attributes', 'References', 'Event PlaceTitle Compare Gramplet']
> # get a reference to my custom Gramplet object
> eptcGramplet = evtview.bottombar.get_children()[5].pui
This returns the Gtk Frame of the first Gramplet in the first column.</pre>|}
<br clear="all"[[Category:Plugins]][[Category:Developers/>General]][[Category:Gramplets]]
22
edits

Navigation menu