Hi,
I wanted a list of modules importable in Pythonista and display corresponding help() text.
As i found help() spewing out text to console for each module cumbersome, i wrote this snippet.
I am sure there must be better ways to doing this. But I am just learning, and
it works :)
Also I learned a little bit about stdout, pkgutil, StringIO in this process.
More Power To Pythonista!
# View help text for all the importable modules
# using StringIO
# Edited based on suggestions by @shtek & @Phuket2
# Edited to make use of contextlib.redirect_stdout
# coding: utf-8
import ui, pkgutil
from io import StringIO
from contextlib import redirect_stdout
w, h = ui.get_screen_size()
fontsize = 15
if w > 767:
fontsize = 24
if w > 1500:
fontsize = 36
modulelist = []
for pkg in pkgutil.iter_modules():
modulelist.append(pkg[1])
def load_action(sender):
ttval = (ttableview1.data_source.items[sender.selected_row])
# redirecting output using contextlib.redirect_stdout
help_str = StringIO()
with redirect_stdout(help_str):
help(ttval)
helptext = help_str.getvalue()
ttextview1.text = helptext
ttextview1 = ui.TextView(name='ttextview1', frame=(w * .3, 0, w * .7, h * .9), flex='WH', text='Click Any Module On Left', border_width=1, border_color=0, font=('<system>', fontsize), bg_color = 'lightyellow', text_color = 'red')
ttextview1.editable = False
ttableview1 = ui.TableView(name='ttableview1', frame=(0, 0, w * .3, h * .9), flex='HR', border_width=1, border_color=0, row_height=h / 20, seperator_color = 'red', alpha = .8)
list_source = ui.ListDataSource(sorted(modulelist))
list_source.font = ('Avenir Next Condensed', fontsize)
list_source.text_color = 'red'
list_source.highlight_color = 'yellow'
ttableview1.data_source = ttableview1.delegate = list_source
ttableview1.data_source.action = load_action
vname = str(len(modulelist)) + ' Modules'
view = ui.View(name=vname, bg_color = 'yellow', frame=(0, 0, w, h * .9))
view.add_subview(ttableview1)
view.add_subview(ttextview1)
view.present(title_bar_color = 'yellow')