Helper script for embedding pyui files in single-file scripts
I've just built a little script to make it easier to share UI scripts. Sometimes, it's just more convenient to have everything in a single file for simple copy/paste – this basically generates code to load the UI from an embedded (compressed) string instead of from an external .pyui file.
Here's the code:
(some more details about how to use it in the docstring)
And this is an example of what the generated code looks like (this is from the bundled ColorMixer example):
data = '''\ QlpoOTFBWSZTWXGZJlkAA4FfgBAAWGd/8D/HGYq/79/6UAOe4DODcAKGkSeRpNPSNlHoTQ AMgAAADSBoaKnsEk0AABoAAABElCmxEyah6T9SGgNqeKGjTQGTR6hzAATAAEwAAAAACpJC NTAp6NNINqADQAaeo00zUeLNIRevYhXCrDnMydv5ddssuKvtW15+Xbtq5Jb7tdp+9zbxAt 0YFOEK1PYeGP1TvLaJaWQwRKSmwu10yMYD+I3AZhIpeImxVjxApqNRWMEpqlaWFSEBpDL/ aufdj7d+bciZInpuffUawrbLDoAvAbnRnsGwjlCwY3AQoiMikcVFxjaN+bHfmJbntv5eF2 +uFa81a57LMdue7TprLZNiPISIBIJIpCEjJJyQUTLnrPqyRNwBBIRSQ6G5UCEdREAQhnkc 6VdTklBZifrGeSmnRkpxuY0EVsxA1pVB5Qsz4MiA4S8EXkSFKFsCzBrVMbE5AonjPMqxNC wkHGbGhkyVvKNgvhUrMzbhyRIMUtxbanSoYg+DVEpEagCDTofplQ3HJlBms7EigcLOq3RN EFnsFtYRbfKW4/Xg+4el7Z5BiEa4QvDTfExOSYnYNrB8bvRTDiUXComARAkIEKYi2uQM0N wquuSlujXUQINEaOD0so0QHiOEwuir0S0zm12Bd4dWkF5BtsG9FSbCDFaughLw0+P7WKlQ DzAdvV2jJ1bfW8DetJUYFS5Uu4fTq9o4DhGuO1X7cZkkZhiMA2oxWh2ilVcKF0RctN1Xou kwaxiLoYw7hbBokOM/0utJ81GN1q71DY/mocm3dN37tJFhz/3wTOL29qnhmhI3MriMuzv2 ugX87PJph2cWzffrVjBZ02cdE6BvZLDeyOedIocsxTdhkSLRyblpDmvSXjP5aUqNoy4N14 syUvKLVLTKNLr1xGhgilLNaVL6cqRkhh0DTObOaIqIqSZhaGcahUjYMBllDUyaHJr8EpNg 7t+MqoZhg357F85kWcrJrJ0yLGcapzSM2mYR5gNDHyATsKOUTCXuuAaYcWnwTbJzAd/ApR Ucg6hwdb9dn84ZR9Rl4V+Zav+LuSKcKEg4zJMsgA== ''' import ui import bz2 from base64 import b64decode pyui = bz2.decompress(b64decode(data)) v = ui.load_view_str(pyui.decode('utf-8'))
Should work in Pythonista 2 and 3 (beta).
TIP this is obvious but nice. In py3, you can easily save a encoded pyui file as a snippet (copy/paste). I tested it to make sure.
I only mention as sometimes the obvious eludes us 😱
It is a nice script for sharing pyui file. We could add a small feature to this. Currently if users want to see or edit the original .pyui file, they may need to write some scripts to decode this embedded pyui file. Here is a small script which generates .pyui file from this embedded pyui file.
It extracts the encoded string, decodes it and generates the .pyui file. Like the embedpyui script,
this needs to be added to editor actions (wrench) menu. I hope it helps.
@abcabc , i am not 100% sure. But it is necessary to strip the triple quotes if you assign to a variable.
When you read the variable, I would have thought you text get the enclosing data.
I have a feeling I am wrong though as @JonB suggested using b85decode/encode if in triple quotes. But I am not sure what I am missing here.
@ Phuket2 , My script decodes exactly the way omz's script does the decoding. Omz's script directly does the load_view afterwards but my script just writes to a file (.pyui). Omz's script has the encoded string in data variable but my script reads the whole code as string (it does not run it and hence it does not have the encoded string) and extracts the encoded string. May be there are better ways to extract but I just use regular expression split to extract the encoded string. The data variable uses triple single quotes and hence that is used for splitting. Instead of extracting you can also modify the code file to write the decoded string to a pyui file. I hope it answers your question.