Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Snippets - Access to back up
-
Does anyone know if there is a way to access the snippets saved in the preferences. I could not find anything in the forum about this. I also used the filenav app from @dgelessus to look around to see if I could see them stored in a folder but not luck. I am getting a little worried as I have lot of them now. I suspect at the moment if it's possible we will be to write a script to access them and back them up. Ideally, I would be nice to have a export and import function built into Pythonista. Depending upon the answers I get, I will write it up on the Pythonista Github page.
-
Snippets are stored as JSON in
os.path.expanduser('~/../Snippets.json')
. -
@omz , thanks. Perfect
-
I really learn something every day I use Pythonista.
Thanks to @Phuket2 , I even didn't know this feature!
I love this app😘 -
@cvp, that's great. I also love it. Snippets are fantastic, I use them all the time. The way they come up in the autocomplete is brilliant. If you take your time and name your snippets to how you think, it's a breeze. I have one called ij_starter, if I just type in starter it pops up. But in that starter file I am ready to go with what I want typically to get going. You could also use the template feature for that also. I prefer the snippet way. But what's great about the snippets is they are copied in place. I have a lot of functions and classes as snippets. Anyway on a mobile system, reduces your typing by a hell of a lot.
-
I just tried a snippet, only to understand how it functions.
Shame on me, I see now how I have lost my time (which is free because I'm retired, but anyway 😬).
I think I'll use this feature instead of templates.
One more time, thanks to let me know. -
I haven't used snippets a lot personally, but I have two that I use quite often:
- An
ifmain
snippet that expands to the standardif __name__ == "__main__"
block - A
doc
snippet that just expands to an empty docstring""""""
and places the cursor inside. This is useful in large files - when typing a docstring by hand, after the first three quotes, the syntax highlighting of the entire file is "inverted" (docstrings are code, and code is docstrings) which takes a while for the syntax highlighter to process. Then when the docstring is closed, the syntax highlighting is inverted again. In large files this can take quite a while, and with the snippet this doesn't happen because all six quotes are inserted at once.
- An
-
@dgelessus , are you using and external keyboard? I only use the on screen keyboard. But really, I have a lot saved as snippets.
Eg. The below, I just save as ij_class_context, but have a lot of other class templates also. To be honest, the thing that makes this so powerful is the way it pops up in the editor as you are typing. The way @omz has implemented the recall/search of the snippets is fantastic. If you had to select your snippet from a ui it would a fraction as useful as it is now.
import ui,copy class MyClass(object): dict_list = [] def __init__(self, *args, **kwargs): pass def __enter__(self): __class__.dict_list.append(copy.copy(self.__dict__)) return self def __exit__(self, type, value, traceback): self.__dict__.update(__class__.dict_list.pop()) if __name__ == '__main__': mc = MyClass() with mc as x: pass
-
Yes, I'm using an external keyboard. Snippets work exactly like normal completion suggestions here as well, so I can expand them with Tab. This is really convenient, I can type
doc
and then Tab, and I can start typing a docstring. -
@dgelessus, ok. But my point was if you use an external keyboard, then the dynamics change. You will feel you can type fast enough on the external keyboard that snippets will not feel,as important to you as if we're using the on screen keyboard. Even though they as just as relevant.
But I am not complaining, you put out some great code. That matters more than how you got there -
Hi, am wondering where the snippets are kept in Editorial and whether it is possible to export or import snippets.
-
Try this on editorial. You can do cut-and-paste. (Or you can write your own import/export based on this code.) May be there are better ways.
#coding: utf-8 import os, glob for f in glob.glob('../Snippets/*.snpt'): print('\n%s\n'%f + open(f).read() + '\n')
Code printing snippet name instead of uuid
#coding: utf-8 import os, json, glob with open('../Snippets/Snippets.edcmd') as fp: json_obj = json.load(fp) snippet_dict = {} for i in json_obj: snippet_dict[i['uuid']] = i['title'] #print(snippet_dict) for f in glob.glob('../Snippets/*.snpt'): fuuid = (f.split('/')[-1]).split('.')[0] fname = snippet_dict[fuuid] with open(f) as fp: print('#####################') print('#'+fname+':') print(fp.read()) print('#####################')