Create text files and send these to other apps?
I'm wondering if the following is possible:
Can python generate a text file and send it to another app? (basically initiate the 'open in' prompt, or do it automatically, and let me choose which app to open the text file in).
For those interested in what I'm trying to do:
Use 'Get Console' App to collect serial data from equipment that is then copied to iOS clipboard
Use 'Pythonista' App to take this unicode string of data and format it
Open formatted data in 'Data Explorer' which accepts *.txt files
I'm trying to see if going from 2 to 3 is possible through Pythonista.
Thanks so much for your help!
If Data Explorer is an iOS app, you can do what you want in Editorial, because it has a function console.quicklook which displays text and lets you 'open in' another app. Pythonista lacks console.quicklook. I made an editorial workflow for this:
console.quicklookis available in Pythonista (since 1.4), I just forgot to update the documentation.
Thanks for your replies!
I do have an account on DropBox, so that might be a solution. The reason I am hesitant is that it isn't officially supported by IT.
You correctly found the data analytics software, it is the first link you provided. It is an iOS app.
Okay, so console.quicklook pointing to a txt file could let me open in the iOS analytics app.
I wouldn't have to transfer the data if Pythonista had numpy (one iOS developer has managed this, the pynum app) and matplotlib (no one has done this), but that is a lot to ask. I would say though, that Pythonista has a beautiful interface, and if it could implement those modules to allow scientific data analysis, a lot of people would be happy (and as an in-app purchase, you could go away w/ lots of $ to support even more development :) ).
- If there is text on the clipboard then write it into a file.
- quicklook() that file to allow the user to "Open In" their second favorite iOS app...
import clipboard, console, os, sys clipboard_text = clipboard.get() if not clipboard_text: sys.exit('No text on the clipboard') out_file_name = sys.argv.rstrip('.py') + '_data.txt' with open(out_file_name, 'w') as out_file: out_file.write(clipboard_text) msg = ' Tap "send" icon at right of titlebar to "Open In" another app. ' console.hud_alert(msg, 'success', 3) console.quicklook(out_file_name) os.remove(out_file_name) # delete out_file as recommended by peterh below
Note: after calling
console.quicklookabove, delete the txt file. Otherwise it stays there (and you probably won't realise)
ccc, thank you! It works perfectly, and was very useful for me to go through as I start Python.
I wanted to also note for anyone that happens to come across this discussion through a search that another App I have found useful for data plotting that works better than Data Explorer currently is 'Graphical' by Vernier. If you use ccc's code to generate a csv instead of a text file, you can open in Vernier and plot it quite easily and select regions of the plot to do a lot of analysis. (now I just have to play with exactly how Vernier wants the data formatted to convert my serial port data to something workable).
Also the Graphical app has some fun applications where you can use the built in iPad sensors to record acceleration for example. I recorded my flight takeoff discretely to see how fast the plane was accelerating. :)
In my efforts, I ran across the following problem: When I use <code>console.quicklook</code> to preview my generated csv file, the 'open in' button does not function for any apps except for the iOS mail client.
<b>Observations:</b> All the icons appear, but when I touch them, it registers the touch and the menu goes away, but nothing happens (except for the mail client, which properly sends the generated csv as an attachment. The attachment can then be opened in the apps that were listed from safari, but this is a round about way).
The first two lines of the CSV file it is working off of are here:
11-23-13 06:26:18 622.80
11-23-13 06:27:18 623.24</code>
My code (forgive me for this, been working with python for a week :) ) is just attempting to find the time in seconds from the first event and create a new csv file that is formatted differently than the one listed above.
csvfilename = time.strftime("%Y%m%d") + '_data.csv'
with open(csvfilename,'rbU') as csvfile:
reader = csv.reader(csvfile,delimiter='\t') new_date_list =  new_val_list =  for row in reader: #create new lists that will be turned into a csv later new_date_list.append(row) new_val_list.append(row)
update_del =  #list for timedelta objects
for i in range(len(new_date_list)):
date_object = datetime.datetime.strptime(new_date_list[i], '%m-%d-%y %H:%M:%S') #creates a timedelta object (difference between two date objects) deltimeobj = date_object -
`` datetime.datetime.strptime(new_date_list, '%m-%d-%y %H:%M:%S')
#generates a new csv file
with open( time.strftime("%Y%m%d") + 'w'+ '_data.csv','wa') as csvfile:
writer = csv.writer(csvfile, dialect='excel') for i in range(len(new_date_list)): writer.writerow([str(update_del[i]),new_val_list[i]])
out_file_name = time.strftime("%Y%m%d") + 'w' + '_data.csv'
msg = ' Tap "send" icon at right of titlebar to "Open In" another app. '
console.hud_alert(msg, 'success', 3)
This code results in a csv file that can be opened only in the mail client, which I then email to myself, open in safari and then open in 'Graphical' by vernier (for example, many other apps open csv files.)
In this example, Graphical by Vernier (and every other app that supports csv files) shows up as an 'open in' option but when touched does not register any action.
Any thoughts would be appreciated :)
I have a similar problem. I am manipulating data files on my ipad and I need to email those files to other people. How can I attach data files to an email with pythonista?
https://docs.python.org/2/library/email-examples.html has good examples and a search on http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python will have a bunch more.
Sending email directly with Python/Pythonista would require that you also keep the SMTP server account information within Pythonista. You can keep the password in the keychain, but it will still be only within Pythonista.
If you just want to mail a single file to somebody from within Pythonista, use
console.open_in()and open it in the mail app. You would have to enter the recipient and mail subject/text in the mail app, though.
For Python scripts you can do the "Open in..." manually via the "Export..." entry in the actions menu. For other file types, you can use the FileNavigator script. I have this always in my action menu. To export any file, I run it from the action menu, navigate to the file, tap the info button and then choose "Open in External App" from the "Actions" section of the file details dialog and open it in the mail app or dropbox.
Update: Sorry, should have verified what I was telling. Actually, "Open in External App..." doesn't give you the mail app as a choice, but if you open a file with Quickview and then tap the "Open in..." button, you get the mail app as an option.