(Post was deleted)
Forum Archive
I call it code helper, but it is a glorified paste applet for the actions menu
Lol, as fate would have it, too many quotes etc in the code to get it to format correctly here. I just made a gist instead,
A view that is presented as a popover. You define the button names in a dict and provide a variable or literal text. If you provide a variable, that variable can be evaluated by a function. It's pretty cool. Nothing I did, just Python being Python. The file looks a mess, don't let that put you off. Seperate the data into another file and import it. I kept it all in one file for ease of showing.
Despite my code, is still a good idea.
I have updated the code a little. Took out some of the more crazy example. Just too many combinations of quotes, it ran correctly , but screwed up the code formatting here.
I was adding buttons to the popup view iterating over a dict. Not great unless you want to play chase the button :) no control the order of buttons that way. So create a sorted list from my dict now.
I know, there is nothing challenging about this code, but exciting for me as its the first real useful code I have written so far. I am using it, and it's nice. I was trying to do something similar with textExpander. I could get it to work consistently. Besides, I prefer this method.
An easy way to test your snippets is to set _debug_mode = True. Rather than just copy to the key, it will also print to the console.
Will try and put this in a repo....operative word being try :)
# @Phuket2
# beware i am a beginner programmer, if you are
# also a beginner programmer, please look over my
# code careful. While i try to be careful, i only
# know what i know
# CodeHelper
# what:
# is a popover view that presents buttons you have described in data below that basically copies text to the clipboard. a gloried copy routine.
# settings:
# 1. _btns_per_line, the number of btns you want displayed on a single row/line
# 2. _btn_w, _btn_h. the width and height of the buttons.
# The popup view, will determine its frame from these vars.
#3._btns, is a dict. {btntitle:varible}
# so an entry in _btns like 'email':'me@gmail.com' will add a
#button called 'email', when clicked will copy me@gmail.com, to the clipboard.
#usage:
# To use this code, you should put in a .py file
# and then add the .py to 'Actions Menu' in
# pythonista.
import ui
import clipboard
import console
import datetime
import calendar
import platform
# if _debug_mode, then as well as copying to the
# clipboad, prints to the console. just for
# debugging and testing your output.
_debug_mode = False
# set the number of rows of btns
_btns_per_row = 1
# set the height and width of the buttons
_btn_h = 60
_btn_w = 120
# some simple examples button variables to output
def make_week_day_header(width = 3):
return str(calendar.weekheader(width))
_text_weekday_header = make_week_day_header()
_text_platform = platform.platform()
_text_date_stamp = str(datetime.datetime.today())
_text_init = '''
def __init__(self):
pass
'''
_text_main = '''
if __name__ == '__main__':
pass
'''
_text_class = '''
class MyClass(object):
def __init__(self):
pass
'''
# key = name of button, value = text, can be a
# literal, from a var defined above of result of
# a funtion that returns a string, losely
# speaking...
_btns ={'init':_text_init,
'main':_text_main,
'class':_text_class,
'email': 'my_email@google.com',
'datestamp' : _text_date_stamp,
'platform' : _text_platform,
'weekday head': _text_weekday_header,
}
#////////////code starts here\\\\\\\\\\\\\\\
# change the buttons apperence here
def make_button(index, the_title):
btn = ui.Button(name=str(index), title=the_title)
btn.name = str(index)
btn.background_color = 'purple'
btn.border_width = .5
btn.tint_color = 'white'
btn.font = ('<system-bold>', 16)
# a little crappy to do like this, but just a quick tool
btn.width = _btn_w
btn.height = _btn_h
return btn
def action(sender):
clipboard.set(_btns[sender.title])
sender.superview.close()
console.hud_alert('Copied', duration = .4)
# print to the console, if in debug_mode
if _debug_mode:
print _btns[sender.title]
def rows_to_write(num_btn, btn_per_row):
#calc the number of btn rows to make
ln,p_ln = divmod( num_btn, btn_per_row )
if p_ln: ln +=1
return(ln)
def add_btns(view, rows):
# make the buttons and set the frames, and action
#create a sorted list, so buttons appear in a predictable order.
ls = sorted((list(_btns)))
for i, item in enumerate(ls):
btn = make_button(i, item)
btn.action = action
w,h = btn.width , btn.height
btn.frame = (i % _btns_per_row * w, i / _btns_per_row * h, w, h)
view.add_subview(btn)
if __name__ == '__main__':
v = ui.View()
v.name = 'Code Helper'
rows =rows_to_write(len(_btns),_btns_per_row )
v.frame = (0,0,_btn_w * _btns_per_row ,_btn_h * rows)
add_btns(v, rows)
v.present('popover')