-
Subject22
Sure is. You just need to know the file path of the external app. Create a script called
test.py
with the following content in Working Copy. Open that repo in Pythonista. Run the script.import os cwd = os.getcwd() print(cwd) with open(f"{cwd}/test.py") as f: print(f.readlines())
This prints the full path to the directory of
test.py
, then reads and prints the contents of that file. Reading other files from the same directory is trivial. Note that this only works for files in folders which you have already opened in Pythonista (Pythonista doesn’t have permission to access any other external files), and that it only works from the main Pythonista app (it won’t work from the widget or share sheet, because those are separate processes with distinct permissions).I see the following output:
/private/var/mobile/Containers/Shared/AppGroup/5BAD0E84-52A4-4FD2-8008-A2508B4F433F/File Provider Storage/Repositories/my-repo ['import os\n', '\n', 'cwd = os.getcwd()\n', 'print(cwd)\n', 'with open(f"{cwd}/test.py") as f:\n', ' print(f.readlines())\n']
-
Subject22
I wondered if there was something undocumented like that! I couldn't find the relevant source code to check though, and my forum-search-foo is lacking. Thank you again!
-
Subject22
Thanks! Someone pointed me to that in the Slack channel too :)
The cleaned up example will be handy.
-
Subject22
@JonB said:
when load_view is called inside init, it knows to bind itself to the calling instance
Oh that's useful! I'll give it a shot when I get a chance. Thanks!
EDIT:
Hang on. I see a problem with that. If I put
ui.load_view()
in the custom view class's__init__
and then attempt to use that view with anotherui.load_view("my_view")
I'll end up in a recursive loop.Does that mean that if I do this I can ONLY instantiate my .pyui-defined views by adding them to other views in interface designer? Or have I missed something again? 😄
-
Subject22
Table cell detail labels are read only, and it doesn't seem possible to adjust them the same way we can cell titles (
TableViewCell.text_label.text = "Hah!"
). Am I missing something, or is there really no way to give table cells detail labels (short of diving head first into ObC-land)? -
Subject22
If I have a bunch of views defined in .pyui files is it possible for me to reuse these to build more complex views?
I see that in the interface designer I can add a custom view and set a custom subclass, but that doesn't pull in the associated .pyui file.
-
Subject22
I don't believe so. I think you have to code in the delegates yourself. It's not too bad though. Something like this should work (NB: Not tested).
import ui class MyTextFieldDelegate(object): def textfield_did_change(self, textfield): print(textfield.text) textfield = ui.TextField() textfield.delegate = MyTextFieldDelegate()
-
Subject22
Please excuse the double post, but this is about a different issue to my last post.
I note that the OP's version of this script assumes that the x-callback response data will be formatted something like
app://xcallbackresponse-REQUEST_ID/?query=value&query=value
. But one of the apps I'm working with formats its response likeapp://xcallbackresponse-REQUEST_IDvalue
which causes the URL parsing in this script to break.Here is a modified version which handles this case a little more gracefully by setting
x_callback_response.parameters
toNone
when it can't parse the URL directly and by creating a newx_callback_response.raw_response_data
which is simply the response URL without theapp://xcallbackresponse-REQUEST_ID
bit. -
Subject22
v2.0
I threw some imports in, but now I'm stuck at:
NameError: global name 'c' is not defined
# coding: utf-8 import x_callback_url from objc_util import ObjCInstance _handler = None _requestID = None url = "working-copy://x-callback-url/status/?repo=MY_REPO&unchanged=1&key=MY_KEY&x-success=pythonista://" def handler(response): print(response) x_callback_url.open_url(url, handler)
EDIT: Ahh. Putting a
from objc_util import *
into my calling module got me over the worst of it. But I still need access to the globals (because with the code above I end up with_requestID
and_handler
beingNone
).EDIT2: Well I'm stumped. I wondered if the leading
_
was messing with the global variables (something something name mangling?) so I renamed_requestID
and_handler
tog_requestID
andg_handler
, respectively and it worked! Everything ran fine after that. A little later I tried reverting that change (the engineer in me likes reproducible errors) and everything still worked fine, despite my earlier problems. So then I tried removing the extra import in my calling module (from objc_utils import *
) and everything still worked fine. So it seems my original issue has vanished without a trace and now I cannot reproduce it.