omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    [share ] crappy code using requests, ui.TableView

    Pythonista
    1
    1
    970
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Phuket2
      Phuket2 last edited by

      Just been trying to help a friend with some example code. It's not good, but still gives some ideas... like food for thought

      # coding: utf-8
      
      
      
      import ui
      import requests, json
      
      _site = '''https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json'''
      
      
      def get_json_data_from_site(website):
      	# generic url request for json data
      	# will fail if the url does not return json data....
      	# can avoid this with some simple checks, but is just an example
      	return requests.get(website).json()
      	
      def make_cell(tv, section, row):
      	data = tv.data_source.items
      	_key = data.keys()[row]
      	cell = ui.TableViewCell()
      	cell.frame = (0,0,ui.get_screen_size()[0] ,44)
      	cell.background_color = data.get(_key)
      	lb = ui.Label(frame = cell.bounds)
      	lb.text = _key
      	lb.alignment = ui.ALIGN_CENTER
      	lb.font = ('Avenir Next Condensed', 28)
      	cell.content_view.add_subview(lb)
      	return cell
      	 
      class DSO (object):
      	
      	def tableview_number_of_sections(self, tv):
      		# Return the number of sections (defaults to 1)
      		return 1
      	
      
      	def tableview_number_of_rows(self, tv, section):
      		# Return the number of rows in the section
      		return len(tv.data_source.items.keys())
      
      	def tableview_cell_for_row(self, tv, section, row):
      		# Create and return a cell for the given section/row
      		return make_cell(tv, section, row)
      		'''
      		cell = ui.TableViewCell()
      		cell.text_label.text = 'Foo Bar'
      		return cell
      		'''
      	'''
      	def tableview_title_for_header(self, tv, section):
      		# Return a title for the given section.
      		# If this is not implemented, no section headers will be shown.
      		return 'Some Section'
      	'''
      
      	def tableview_can_delete(self, tv, section, row):
      		# Return True if the user should be able to delete the given row.
      		return True
      
      	def tableview_can_move(self, tv, section, row):
      		# Return True if a reordering control should be shown for the given row (in editing mode).
      		return True
      
      	def tableview_delete(self, tv, section, row):
      		# Called when the user confirms deletion of the given row.
      		pass
      
      	def tableview_move_row(self, tv, from_section, from_row, to_section, to_row):
      		# Called when the user moves a row with the reordering control (in editing mode).
      		pass
      
      
      if __name__ == '__main__':
      	
      	json_data = get_json_data_from_site(_site)
      	
      	# setting f to the size of the screen
      	f = (0,0, ui.get_screen_size()[0], ui.get_screen_size()[1])
      	v = ui.View(frame = f)
      	v.name = 'css names - {} colours'.format(len(json_data.keys()))
      	tbl = ui.TableView()
      	tbl.frame = v.bounds
      	tbl.data_source = DSO()
      	tbl.data_source.items = json_data
      	v.add_subview(tbl)
      	
      	v.present('')
      	
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post
      Powered by NodeBB Forums | Contributors