Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
this is crap. i mashed to files together without editing.
but i could be something. i have just been to lazy to bring it
together. i use it the wrench menu myself.
it acually works for meas a quickie when i am looking for a script.
import ui, editor, console, editor
import io, re
# coding: utf-8
import ui, os, scene, io
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.styles import get_style_by_name
from pygments.lexers import get_lexer_by_name
from pygments.styles import get_all_styles
# last gist update, 00:10 17th Feb, 2016
class CodeDisplay(ui.View):
def __init__(self, style = 'colorful', use_linenos = True,
hl_lines = None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.code = ''
self.code_raw = ''
self.web_view = None = style
self.use_linenos = use_linenos
self.hl_lines = hl_lines if hl_lines else list()
def make_view(self):
self.web_view = ui.WebView()
self.web_view.frame = self.bounds
self.web_view.scales_page_to_fit = True
self.web_view.flex = 'wh'
def get_style_names(self):
#return [s for s in get_all_styles()]
# i stopped with list comp. i think get_all_styles(), was causing
# Pythonista to crash. tried many times. its was during editing, so
# i think some code executing on the import. creating the problem.
# but just guessing
lst = []
for style in get_all_styles():
return sorted(lst)
def load_code_str(self, code):
# load python code as a string into the webview
self.code = code
def load_code_from_file(self, fn):
# load a file into the webview
code = ''
with, encoding = 'utf-8') as fp:
code =
self.code = code
def reload_style(self):
# stupid hack for now. we rethink this.
def hilite_lines_with_word(self, word):
if not word:
r = []
w = word.lower()
for ln, data in enumerate(self.code.splitlines()):
if w in data.lower():
r.append(ln + 1)
self.hl_lines = r
def get_html(self, code, use_linenos = None,
style = None, hl_lines = None):
# returns the html from pygments
if not use_linenos: use_linenos = self.use_linenos
if not style: style =
if not hl_lines : hl_lines = self.hl_lines
lexer = get_lexer_by_name("python", stripall=False)
formatter = HtmlFormatter(linenos= use_linenos , style = style, hl_lines =hl_lines, full=True)
return highlight(code, lexer, formatter)
def preview_Code(fn, word_hilite, *args, **kwargs):
cd = CodeDisplay(*args, **kwargs)
return cd
def called_from_tools_menu():
if called from the tools/wrench menu, we just view the current script.
As i normally use a dark theme, this is ok for me sometimes
f = (0,0, 600, 800)
cd = preview_Code('','root', frame = f)
path = ''
import editor, os
multipler = .8
f = (0,0, ui.get_screen_size()[0] * multipler, ui.get_screen_size()[1] * multipler)
#cd = CodeDisplay(frame = f , hl_lines = range(10, 25))
cd = CodeDisplay(frame = f )
cd.load_code_from_file(path) = fn
#from pygments_helper import preview_Code
Pytonista Forum user @Phuket2
Python ability - beginner
#_ignore_dirs = ['.Trash', 'Standard Library']
#skip_dirs = '\\.Trash|site-packages|Backup'
skip_dirs = '\\.Trash|Backup'
exts = 'py'
root_dir = os.path.expanduser('~/Documents')
def shorten_path(path, inc_root=False):
# return minus what we consider the visible root
if inc_root:
return path[len(os.path.join(os.path.expanduser('~'), 'Documents')):]
return path[len(os.path.expanduser('~')):]
def allfiles(root_dir, skip_dirs_re=None, file_ext_re=None):
# from @omz on Pythonista Forums
for path, subdirs, files in os.walk(root_dir):
if skip_dirs_re:
new_subdirs = []
for subdir in subdirs:
if not re.match(skip_dirs_re, subdir):
subdirs[:] = new_subdirs
for filename in files:
ext = os.path.splitext(filename)[1][1:]
if (file_ext_re is None) or (re.match(file_ext_re, ext, re.IGNORECASE)):
full_path = os.path.join(path, filename)
yield full_path
def search_file2(fspec, text):
with, 'r', encoding='utf-8') as file:
if,, re.IGNORECASE):
return True
return False
def search_file(fspec, text, ignore_case = True):
with, 'r', encoding='utf-8') as file:
for ln in file.readlines():
if comp_IGNORECASE(text, ln):
return True
return False
def comp_IGNORECASE(text, data):
if text.lower() in data.lower():
return True
return False
class SearchDisplay(ui.View):
def __init__(self, file_list, search_term, *args, **kwargs):
super().__init__(*args, **kwargs)
self.file_list = file_list
self.search_term = search_term = None
self.code_preview = None
self.current_file_name = None
m_btn = ui.ButtonItem(title='Open In Editor')
m_btn.action = self.menu_open_in_editor
self.left_button_items = (m_btn,)
self.right_button_items = ()
m_btn = ui.ButtonItem(title='Go Back')
m_btn.action = self.menu_goback
m_btn.enabled = False
self.right_button_items = (m_btn,) = 0
self.current_file_name = self.file_list[0]
def make_view(self):
tv = ui.TableView(frame=self.bounds)
tv.flex = 'wh'
tv.data_source = ui.ListDataSource(self.file_list)
tv.data_source.tableview_cell_for_row = self.tableview_cell_for_row
tv.data_source.accessory_action = self.accessory_action
tv.delegate = tv.data_source = tv
def update_title(self): = 'Number of Results = {}'.format(len(
def menu_goback(self, sender):
if self.code_preview:
self.code_preview = None
self.right_button_items[0].enabled = False
def menu_open_in_editor(self, sender):
if self.current_file_name:
editor.open_file(self.current_file_name, True)
def tableview_cell_for_row(self, tableview, section, row):
# Create and return a cell for the given section/row
cell = ui.TableViewCell('subtitle')
item = tableview.data_source.items[row]
cell.text_label.text = os.path.basename(item)
cell.detail_text_label.text = shorten_path(item)
cell.accessory_type = 'detail_button'
return cell
def accessory_action(self, sender):
row = sender.tapped_accessory_row
self.current_file_name = sender.items[row] = row
def show_preview(self, fspec):
cp = preview_Code(fspec, self.search_term, frame=self.bounds) = os.path.basename(fspec)
self.code_preview = cp
self.right_button_items[0].enabled = True
def find_in_my_source():
s_txt = console.input_alert('Enter Search String')
if not s_txt:
lst = []
for fn in allfiles(root_dir=root_dir, skip_dirs_re=skip_dirs,
file_ext_re=exts ):
if search_file(fn, s_txt):
if not len(lst):
console.alert('No Natch Found')
mc = SearchDisplay(lst, s_txt, frame=f, bg_color='white')
mc.present(pres, animated=False)
if __name__ == '__main__':
w = 600
h = 800
f = (0, 0, w, h)
pres = 'sheet'
s_txt = console.input_alert('Enter Search String')
if not s_txt:
lst = []
for fn in allfiles(root_dir=root_dir, skip_dirs_re=skip_dirs, file_ext_re=exts ):
if search_file(fn, s_txt):
print('Num files found= ', len(lst))
if not len(lst):
console.alert('No Natch Found')
#x = MyDataSource(_files)
mc = SearchDisplay(lst, s_txt, frame=f, bg_color='white') = x = x
mc.present(pres, animated=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment