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.
A copy code button here in the forum.
-
Maybe I am just doing something so wrong. I hope so. But I find it so hard to copy any code from the forum onto the clipboard. A small block is generally ok, a larger block of code I can normally get it, but hit and miss for me.
So assuming I get the code on the clipboard and paste it into pythonista, all is ok running the code that is until I make one change. Then I start getting all the indent errors. The only way I know to fix it is to go through and delete all the spaces and replace them with tabs by hand. Very frustrating.I would appreciate if anyone knows a very simple method of doing this.
But it seems it would be great if there was a copy button below the code blocks that copied the code in a way pythonista would not have issues with edits.
I really don't know why it's taken me so long to mention this. I guess, it's really driving me crazy now. I think to be able to copy code here smoothly and edit it in pythonista is very important for the community so we can help each other.
Anyway, any advice appreciated
-
The only way I know to fix it is to go through and delete all the spaces and replace them with tabs by hand. Very frustrating.
Have you tried using "Convert Indentation" after pasting code?
-
Lol, the smartest man I have ever known or met (my boss before I retired) taught me never to pose a question you don't know the answer to (in business that is). I am sure you knew the answer :)
Hmmm, you are so right! I have probably seen that item in the menu 1,000's of times and it just was there and never sunk in to my brain. I just did a real world example from the forum, worked perfectly. As a new guy, I often miss simple things like this. I am not relaxed, not confident. Sort of blinds you.
I would also suggest to remind people to go back and look at your sample code. I have just been looking through the samples again before your reply arrived. I am going to use your colormixer sample as a popover to set the colors in my preferences page for my movie app I am working on, I am sure it can be done better, but for now it will suffice. It's a starting point.
I seen other cool stuff in your sample programs also. The problem is that when I first looked at your samples, I didn't understand most if it. Just wetted my appetite for what could be done. So I think worth while reminding to newbies like me to go back and look at the samples once you think you are making some progress.
But thanks again, this simple answer will save me so much time. I am sure, will be the same when I copy code from github.
-
I'm working on a UI for interacting with the forum that can be used in Pythonista. (I guess I don't like using websites sometimes??)
This was one thing I thought should be included> copy code to new py file. I found it easier to copy code in the UI so far- didn't test running it though.
It's certainly not impossible. :)
-
I just cobbled together this little helper script. If a forum URL is in the clipboard, it extracts all code blocks from the page... If you're running the 1.6 beta, you can also use it in the extension, directly from Safari (no need to copy the URL in that case).
import clipboard use_appex = False try: # appex is only available in 1.6 beta, fail gracefully when running in 1.5 import appex use_appex = appex.is_running_extension() except: pass def main(): if use_appex: url = appex.get_url() else: url = clipboard.get() if 'omz-forums' not in url: print 'No forum URL' return import requests import bs4 html = requests.get(url).text soup = bs4.BeautifulSoup(html) pre_tags = soup.find_all('pre') if pre_tags: text = ('\n#%s\n\n' % ('=' * 30)).join([p.get_text() for p in pre_tags]) clipboard.set(text) print 'Code copied (%i lines)' % (len(text.splitlines())) else: print 'No code found' if __name__ == '__main__': main()
-
Oh that is so cool!!
-
@ccc, yes it's very cool. But your mission is, if you accept it, is to bring up all the code blocks in the post and allow the user to select which code block they want to copy to the clipboard :)
This msg, may never self destruct:)
Sorry, my sad sense of humour -
Guys, I tried to put a ui wrapper around omz code. Seems to work ok. But, if you copy the code to the clipboard and run the app again, just hangs. I should not get pass the checks. Something very weird is going on. My wrapper is not pretty, I was trying to change as little as possible due to lack of understanding. Hopefully, I am just doing something really stupid. I did comment out the the code in the try block for appex, just in case that was causing a problem. I didn't appear to be that. Any help appreciated.
# coding: utf-8 # WARNING - HANGS AFTER COPY CODE, THEN RUNNING # AGAIN WITH THE COPIED CODE ON THE CLIPBOARD. # HAVE TO RESTART PYTHONISTA! import ui import clipboard import console class OmzFourmCopy(ui.View): def __init__(self, frame, data_list): self.frame = frame self.data = data_list # create copy title btn btn = ui.ButtonItem('Copy Code') btn.action = self.copy_code self.right_button_items = [btn] # create the tableview, set the data_source # and delegate to self. tv = ui.TableView(name = 'tv1') tv.name = 'tv1' tv.data_source = self tv.frame = (0,0,self.width * .20, self.height) tv.border_width = .5 tv.delegate = self self.add_subview(tv) #create textview txtv = ui.TextView(name = 'txtv') txtv.frame = (tv.width, 0, self.width - tv.width, self.height) txtv.font = ('Courier', 12) txtv.text = '' self.add_subview(txtv) tv.selected_row = 0 self['txtv'].text = self.data[0] def copy_code(self, sender): if self['txtv'].text: code = self.data[self['tv1'].selected_row[0]] #clipboard.set(self['txtv'].text) clipboard.set(code) console.hud_alert('Copied', duration = .5) self.close() def tableview_cell_for_row(self, tableview, section, row): cell = ui.TableViewCell() cell.text_label.text = 'Code {}'.format(row + 1) return cell def tableview_number_of_rows(self, tableview, section): # Return the number of rows in the section return len(self.data) def tableview_did_select(self, tableview, section, row): self['txtv'].text = self.data[row] use_appex = False try: # appex is only available in 1.6 beta, fail gracefully when running in 1.5 import appex use_appex = appex.is_running_extension() except: pass def main(): lst = [] if use_appex: url = appex.get_url() else: url = clipboard.get() if 'omz-forums' not in url or len(url.splitlines()) > 1: #print 'No forum URL' lst.append('-1') # i know this is crappy. return lst import requests import bs4 html = requests.get(url).text soup = bs4.BeautifulSoup(html) pre_tags = soup.find_all('pre') if pre_tags: text = '' #text = ('\n#%s\n\n' % ('=' * 30)).join([p.get_text() for p in pre_tags]) for p in pre_tags: lst.append(p.get_text()) #clipboard.set(text) #print 'Code copied (%i lines)' % (len(text.splitlines())) else: #print 'No code found' pass return lst if __name__ == '__main__': f = (0,0, 540,576) lst = main() if len(lst) == 0: console.alert('No code found') elif lst[0] == '-1': console.alert('omz-forums, not in url') else: x = OmzFourmCopy(f, lst) x.present('sheet')
-
@cook, how far along are you with your tool? You can see the I am having a weird problem with the wrapper I did around omz code. If nothing else, I will still work on this code for myself. Without the weird bug, it's nice and easy. But I can imagine many in the community would like a tool like this. But would be better done by someone with more experience than me to make it rock solid, also pulling out additional information from the html etc. so just saying, if you need a tester etc, happy to help. I will only keep on this until someone says they are working on a robust version of a similar tool. Which I think will happen.
-
It's hard to say how far along I am. Currently I'm working on getting all the UI together in a nice and logical way. I definitely do not have more experience. I've been learning Python for about 2 months now. I decided to take the plunge and get Pythonista (didn't know how to code in Python before I got it). I love it. Anyway- I have no idea what I'm doing so things take a long time.
Here's what I'm working on - maybe around 60% done:
The main view has current activity on the forum (from the rss). It looks similar to the iOS mail app> Tap on a tableview item on the left side, it opens up in a view on the right.
The interactivity with the forum will come with these diffierent functions:
- ability to view an entire thread. (initial view will be what is in the RSS, but then a button tap will lead to viewing the original thread)
- ability to reply/comment on a thread (either while viewing the thread or the initial RSS item)
- ability to post something new, with markdown preview (I made this before)
- ability to copy code from a post/comment into a new file or clipboard. It will function much like what Ole wrote out above, but no need to copy a link. One challenge of course is copying the code while viewing the thread - sometimes there are many code blocks!. :)
The unfortunate part about using the RSS is lack. It has 20 entries. It also doesn't contain the URL for the original thread so there is a lot of fetching required. If it's possible to grab this feed with even 100 entries that would be great. But having the URL for the original thread would be really nice. I don't want Ole to be bogged down with silly little things. There's bigger fish to fry.
-
The docs over at Bootstrap use a nice copy button built into all their code, I imagine that there is some sort of plugin for this. Investigating...
-
@cook, sounds interesting. And to be clear, I am very much a beginner. I am old and retired, so I am not going at any break neck pace to learn, just doing it in my own time. But love learning python also. Especially in pythonista.
Understand if you are not ready to publish your code yet, but it would be nice if you can do some screen shots. I am not sure about the RRS feed, I am guessing the number 20 is just a single constant somewhere. I am sure easy to change, but maybe there other issues such as bandwidth issues etc. I did a little with RRS feeds, I think most important thing is to save the servers response so you are not doing redundant downloads from the server.
With omz code, is easy to pull out all the code blocks also. Also, there is one nice thing with clipboard processing, that is using workflow. I did a simple workflow in the workflow app, to copy the URL from safari, put it on the clipboard then call the code above. So if you are in the forum, looking at a post, a few taps and you have all the code blocks ready to view and copy in pythonista.
Btw, are you using 1.5 or 1.6? I am on 1.5. Not sure how all this changes in 1.6.
Again, if I can be any help, I will do my best -
I can't remember how to insert the Dropbox image inline in the forum :(
-
Dropbox image inline in the forum
?raw=1
# what could be more Phuket than that?!? -
@Phuket2
Here's a screenshot. My code is a bit of a jumble yet- I fear sharing it will cause some professional coders to have a panic attack!! hahaAnyway here's a preview: (let's see if I can get that inline image to work haha)
-
Awesome, can't wait
-
@cook, looks nice. I hope something can be done about the RSS limitation. There are some other limitations also. If I search for my user name for example, I don't get all the listings back. I have a feeling connected to the 20 limitation somehow. Frustrating, because I get some good answers sometimes and can't find them. Eg, how to put an inline Dropbox in the forum :)
Still can't figure it out. Still early in the day for me :) will try later, as well as what ccc says, I remember need to use ![] somewhere also.
With your browser, are you storing the posts locally as well? Not sure it exactly makes sense to do so. Could could be nice to mark favourite posts etc for reference, for retiree's like me with a failing memory :)
Also, is the Table you are using a std ui.TableView?
Your ui is looking nice and professional. -
Same code, as above. No fixes for the weird bug yet. As I say as long as the clipboard is valid, works nicely. Even better invoking it from WorkFlow.
I have just updated positioning code into layout, instead of doing a sheet view. Comes up in full screen now and is correct on my iphone 6 and iPad and adjusts with orientation.
I have to start to do this all the time. Just a mind set.# coding: utf-8 # WARNING - HANGS AFTER COPY CODE, THEN RUNNING # AGAIN WITH THE COPIED CODE ON THE CLIPBOARD. # HAVE TO RESTART PYTHONISTA! import ui import clipboard import console class OmzFourmCopy(ui.View): def __init__(self, data_list): self.data = data_list # create copy title btn btn = ui.ButtonItem('Copy Code') btn.action = self.copy_code self.right_button_items = [btn] # create the tableview, set the data_source # and delegate to self. tv = ui.TableView(name = 'tv1') tv.name = 'tv1' tv.data_source = self tv.border_width = .5 tv.delegate = self #create textview txtv = ui.TextView(name = 'txtv') txtv.font = ('Courier', 14) # select the first code block tv.selected_row = 0 txtv.text = self.data[0] self.add_subview(txtv) self.add_subview(tv) def copy_code(self, sender): if self['txtv'].text: code = self.data[self['tv1'].selected_row[0]] #clipboard.set(self['txtv'].text) clipboard.set(code) console.hud_alert('Copied', duration = .5) self.close() def layout(self): self.frame = self.bounds tv = self['tv1'] tv.frame = (0,0,self.width * .20, self.height) self['txtv'].frame = (tv.width, 0, self.width - tv.width, self.height) def tableview_cell_for_row(self, tableview, section, row): cell = ui.TableViewCell() cell.text_label.text = 'Code {}'.format(row + 1) return cell def tableview_number_of_rows(self, tableview, section): # Return the number of rows in the section return len(self.data) def tableview_did_select(self, tableview, section, row): self['txtv'].text = self.data[row] use_appex = False try: # appex is only available in 1.6 beta, fail gracefully when running in 1.5 import appex use_appex = appex.is_running_extension() except: pass def main(): lst = [] if use_appex: url = appex.get_url() else: url = clipboard.get() if 'omz-forums' not in url or len(url.splitlines()) > 1: #print 'No forum URL' lst.append('-1') # i know this is crappy. return lst import requests import bs4 html = requests.get(url).text soup = bs4.BeautifulSoup(html) pre_tags = soup.find_all('pre') if pre_tags: text = '' #text = ('\n#%s\n\n' % ('=' * 30)).join([p.get_text() for p in pre_tags]) for p in pre_tags: lst.append(p.get_text()) #clipboard.set(text) #print 'Code copied (%i lines)' % (len(text.splitlines())) else: #print 'No code found' pass return lst if __name__ == '__main__': lst = main() if len(lst) == 0: console.alert('No code found') elif lst[0] == '-1': console.alert('omz-forums, not in url') else: x = OmzFourmCopy(lst) x.present()
-
I hope something can be done about the RSS limitation. There are some other limitations also. If I search for my user name for example, I don't get all the listings back. I have a feeling connected to the 20 limitation somehow. Frustrating, because I get some good answers sometimes and can't find them.
I see that too. Limited response from the server.
how to put an inline Dropbox in the forum
Markdown format for inline image is like this: <code>! [some text] (image url)</code>
For dropbox image you need to modify the url:
<code>https://www.dropbox.com/s/00e5iys2alnuzxs/forumspreview.png?dl=0</code>
minus "dl=0" plus "raw=1" equals this:<code>https://www.dropbox.com/s/00e5iys2alnuzxs/forumspreview.png?raw=1</code>
Then finally in markdown:
<code>! [my picture] (https://www.dropbox.com/s/00e5iys2alnuzxs/forumspreview.png?raw=1)</code>
Just no spaces in between!With your browser, are you storing the posts locally as well? Not sure it exactly makes sense to do so. Could could be nice to mark favourite posts etc for reference, for retiree's like me with a failing memory :)
Plan to. I want to include a 'following thread' type of thing - for threads that are interesting to you and may or may not be active in the RSS feed. I also thought about including a kind of bookmark feature for posts/threads that you just want to keep around for reference' sake.
Also, is the Table you are using a std ui.TableView?
Yes. The tableviewcells include a few different textviews. It took me a long time to get it this way. I also experimented with webview inside the tableviewcell - it was okay but the selection highlighting didn't work out. I'd have to change the background of the html - which is possible but a pain I think.
-
@cook, the bookmark and follow thread ideas sound great. There really is such a wealth of info on the forum.
Note to @omz (would be great if all searches yielded all results. Of course newbies will keep asking the same questions if they search the forum and no results come back. I am sure many of the questions I have asked before have been answered, but I don't find any reference to them when I search).
@cook, I am not sure that the @ is put to any use in the forum currently. Not to say it won't be, but another possibility for indexing.
For the local data store are you thinking about SQLite or something simpler like pickled dicts?Thanks, finally got the inline Dropbox pics posting worked out :) i will ask you, I have asked here before, but no answers. On iOS what do you use to save titbits of info relating to Python and Pythonista? I can't find a decent if any snippet manager. I have dash, can't save snippets in it directly, I have Evernote, OneNote and many others. None really do it for me. I have tried using textexpander in pythonista, but it's a hit and miss thing (unless there is a magic setting I missed). Anyway, if you use something to help you remember all the stuff you are learning, I would be great full to hear about it.
Yes, I also found out about the magic tableview cells :) subtitle, Value1, Value2. Is nice what you can do, I also had problems with the hilighting once I started added my own objects to the content_view of the cell. It was not important at the time, but will be soon. Not sure you know or are interested, but you are called back often to create cells. I thought it was being all buffered internally, asked to create the cell once. I was doing so tests yesterday with sections and printing out the section,row when I was called to make a cell. It's a good thing, I just didn't think it worked that way.
Again, if I can help in my limited capacity , please let me know.For now, I will continue with the interface around omz scraping code. It's very different from what you are doing. I am using it all the time here now. I will add a create new file also to a particular directory rather than just the copy to clipboard. The copy to clipboard is almost useless if you can write it straight to a file.
Thanks again for your reply.