Forum Archive

Import Pythonista files in Editorial workflow

charmaex

Is there a way to import a Pythonista file into Editorial? A simple py file is easy but I want to import a py file with a pyui.
Is there a way?

ccc

I was able to get the following to work:

  1. Copy the workflow Save to Python...
  2. Create a new workflow from the clipboard
  3. Edit the Python code in that workflow to change both instances of ".py" to ".pyui"
  4. Save the workflow as "Save to Pyui..."
  5. Copy the text of a Pythonista .pyui file
  6. Paste that text into the Editorial editor
  7. Run the "Save to Pyui..." workflow

Step 5 is easier in the Pythonista v1.6 beta (just rename the file from x.pyui to x.json) but you should be able to the following in all versions of Pythonista.

with open('x.pyui') as in_file:
    print(in_file.read())
charmaex

Thank you for the help.

I think I haven't explained it well.

I have a python script with ui which I would like to run as an Editorial workflow.

As far as I understand your solution it is to get a pyui file saved in Editorial?

ccc

If you are building your user interface at runtime with Python commands (e.g. without a .pyui file) then it is much easier.

Paste the following into an Editorial "Run Python Script":

#coding: utf-8
import ui, workflow

action_in = workflow.get_input()

#TODO: Generate the output...
action_out = action_in

def button_action(sender):
    global action_out
    action_out = sender.title
    sender.close()

button = ui.Button(title='Boom!')
button.action = button_action
button.width = 120
button.height = 32
button.present('sheet', hide_title_bar=True)
button.wait_modal()

workflow.set_output(action_out)

Alternatively, you could use the workflow Save to Python... to put my_ui_script.py into place and then your "Run Python Script" would be:

#coding: utf-8
import my_ui_script, workflow
workflow.set_output(my_ui_script.main(workflow.get_input()))
charmaex

Okay.
As I already have a pyui file - is there a way to convert a pyui into python commands?

ccc

If you already have a .pyui file then the first step is to do what I recommended in my first post above. This will get your .pyui file into Editorial. Then use Save to Python to move your Python file into Editorial. Finally, use the two line script above to import and run your Python file's main() function.

Alternatively, there is a way to put a .pyui file's data into a Python variable. See: pyui_variable.py and https://omz-forums.appspot.com/pythonista/post/5254558653612032

charmaex

Thanks for your help.
I finally copied the text from the pyui file and create a __temp.pyui.