A web framework for aspiring media terrorists – PRE-ALPHA – DO NOT DEPLOY!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/usr/bin/env python
  2. import math
  3. import random
  4. import json
  5. import requests
  6. import datetime
  7. import flask
  8. import numpy
  9. import pandas
  10. import poobrains
  11. from flask import redirect
  12. app = poobrains.app
  13. @app.route('/')
  14. def front():
  15. return redirect(News.url())
  16. class TestSubForm(poobrains.form.Fieldset):
  17. oink = poobrains.form.fields.Text(label="OMGWTF")
  18. foo = poobrains.form.fields.Text(label="SUBfoo")
  19. submit = poobrains.form.Button('submit', label="SUBSUBMIT")
  20. @app.expose('/form')
  21. class TestForm(poobrains.form.Form):
  22. foo = poobrains.form.fields.Text()
  23. bar = TestSubForm()
  24. optin = poobrains.form.fields.Checkbox(label="Opt-in", default=False, required=True, choices=[(True, None)])
  25. radio = poobrains.form.fields.Radio(type=poobrains.form.types.INT, choices=[(1, 'One'), (5, 'Five'), (23, 'Twentythree'), (42, 'Fortytwo')])
  26. multicheck = poobrains.form.fields.Checkbox(label="Check 'em", type=poobrains.form.types.STRING, choices=[('dubs', 'dubs'), ('trips', 'TRIPS'), ('quads', 'QUADS!1!!!!')], multi=True)
  27. completeme = poobrains.form.fields.Text(label="Lookit me, I can autocomplete without JS!", choices=[('Mr. Foo', 'foo'), ('Mr. Florb', 'florb'), ('Ms. Bar', 'bar')])
  28. ranged = poobrains.form.fields.Range()
  29. trigger = poobrains.form.Button('submit', label='Hit me!')
  30. def process(self, submit):
  31. flask.flash('TestForm.process called!')
  32. return self
  33. @app.expose('/news', mode='full')
  34. class News(poobrains.commenting.Commentable):
  35. """ This is the news class docstring """
  36. class Meta:
  37. search_fields = ['title', 'name', 'text']
  38. title = poobrains.storage.fields.CharField()
  39. text = poobrains.md.MarkdownField()
  40. @app.expose('/paste', mode='full', title='Copypasta')
  41. class Paste(poobrains.tagging.Taggable):
  42. type = poobrains.storage.fields.CharField()
  43. text = poobrains.storage.fields.TextField()
  44. @app.site.box('menu_main')
  45. def menu_main():
  46. menu = poobrains.rendering.Menu('main')
  47. try:
  48. News.permissions['read'].check(flask.g.user)
  49. menu.append(News.url(), 'News')
  50. except poobrains.auth.AccessDenied:
  51. pass
  52. try:
  53. Paste.permissions['read'].check(flask.g.user)
  54. menu.append(Paste.url(), 'Pastes')
  55. except poobrains.auth.AccessDenied:
  56. pass
  57. for url, caption in poobrains.auth.Page.main_menu_entries():
  58. menu.append(url, caption)
  59. return menu
  60. class NonExposed(poobrains.auth.Administerable):
  61. text = poobrains.storage.fields.TextField()
  62. class NonExposedB(NonExposed):
  63. pass
  64. class AVeryVeryLongNameToTestMenuPositioning(poobrains.auth.Administerable):
  65. florp = poobrains.storage.fields.BooleanField()
  66. #class MultiPK(poobrains.auth.Administerable):
  67. #
  68. # class Meta:
  69. # primary_key = peewee.CompositeKey('pk_a', 'pk_b')
  70. #
  71. # pk_a = poobrains.storage.fields.IntegerField()
  72. # pk_b = poobrains.storage.fields.IntegerField()
  73. #
  74. #
  75. #class NestedHandle(poobrains.auth.Administerable):
  76. #
  77. # class Meta:
  78. # primary_key = peewee.CompositeKey('foreign', 'local')
  79. #
  80. # foreign = poobrains.storage.fields.ForeignKeyField(MultiPK)
  81. # local = poobrains.storage.fields.CharField()
  82. @app.site.listing(NonExposedB, '/custom', mode='full', title='Custom Listing')
  83. def list_nonexposed(listing):
  84. return listing
  85. class NASAApproaches(poobrains.svg.plot.Dataset):
  86. def fill(self, spkid=2099942): # default spkid for apophis
  87. try:
  88. spkid = int(spkid)
  89. except ValueError:
  90. raise poobrains.errors.ValidationError("Invalid SPK ID; must be an integer.")
  91. response = requests.get('https://api.nasa.gov/neo/rest/v1/neo/%d?api_key=DEMO_KEY' % spkid)
  92. if response.status_code != 200:
  93. raise poobrains.errors.ValidationError("NASA API responded with error code %d." % response.status_code)
  94. data = json.loads(response.text)
  95. self.title = "Close approaches of %s" % (data['name_limited'] if 'name_limited' in data else data['name'])
  96. self.description = "**%s** belongs to orbit class **%s**; %s" % (data['name'], data['orbital_data']['orbit_class']['orbit_class_type'], data['orbital_data']['orbit_class']['orbit_class_description'])
  97. for approach in data['close_approach_data']:
  98. x = float(approach['epoch_date_close_approach'])
  99. y = float(approach['miss_distance']['kilometers'])
  100. self.add_datapoint(x, y)
  101. class StockWeekly(poobrains.svg.plot.Dataset):
  102. def fill(self, symbol='FB'):
  103. response = requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=%s&apikey=%s' % (symbol, app.config['ALPHAVANTAGE_API_KEY']))
  104. if response.status_code != 200:
  105. raise poobrains.errors.ValidationError("AlphaVantage API responded with error code %d." % response.status_code)
  106. data = json.loads(response.text)
  107. dates = [x for x in data['Weekly Time Series'].keys()]
  108. first_date = dates[-1]
  109. last_date = dates[0]
  110. self.title = symbol
  111. self.description = f"Data from {first_date} to {last_date}"
  112. for datestring, datapoint in data['Weekly Time Series'].items():
  113. y, m, d = [int(x) for x in datestring.split('-')]
  114. date = datetime.datetime(y, m, d)
  115. x = date.timestamp()
  116. y = float(datapoint['1. open']) + float(datapoint['4. close']) / 2
  117. error_lower = y - float(datapoint['3. low'])
  118. error_upper = y - float(datapoint['2. high'])
  119. self.add_datapoint(x, y, error_lower=error_lower, error_upper=error_upper)
  120. class ConstrainedRandom(poobrains.svg.plot.Dataset):
  121. def fill(self, magnitude: int = 2, length: int = 16) -> None:
  122. #app.debugger.set_trace()
  123. magnitude, length = int(magnitude), int(length)
  124. ranges = []
  125. for i in range(0, magnitude):
  126. ranges.append(sorted((random.random(), random.random())))
  127. #rawdata = pandas.Series(name=self.__class__.__name__, index=[0])
  128. rawdata = []
  129. for i in range(0, length):
  130. y = random.random()
  131. for r in ranges:
  132. y = r[0] + (r[1] - r[0]) * y
  133. r = ranges[round((len(ranges) - 1) * random.random())] # select random range
  134. y = r[0] + (r[1] - r[0]) * y
  135. #y %= (i + 1)
  136. #y = i % y
  137. y *= math.sin(2 * math.pi * float(i) / length)
  138. #self.add_datapoint(i, y) #, error_lower=y - r[0], error_upper=r[1] - y)
  139. #rawdata.iat[i] = y
  140. rawdata.append(y)
  141. #self.data.iat[0] = rawdata
  142. self.data = pandas.DataFrame(data=rawdata)
  143. class Sine(poobrains.svg.plot.Dataset):
  144. def fill(self, length=10):
  145. import pudb; pudb.set_trace()
  146. inc = (2 * math.pi) / (length - 1)
  147. #inc = 1.0 / (length - 1)
  148. rawdata = {}
  149. x = 0
  150. for _ in range(0, length):
  151. rawdata[x] = math.sin(x)
  152. x += inc
  153. self.data = pandas.DataFrame(columns=['sine'], index=rawdata.keys(), data=rawdata.values())
  154. class RandomMap(poobrains.svg.geo.GeoData):
  155. def fill(self):
  156. data = poobrains.svg.geo.geojson.MultiPolygon()
  157. for poly_num in range(0, random.randrange(1, 8)):
  158. points = []
  159. for point_num in range(0, random.randrange(3, 7)):
  160. points.append((random.randrange(-180, 181),random.randrange(-75, 76)))
  161. data.coordinates.append([points])
  162. self.data = poobrains.svg.geo.geojson.dumps(data)
  163. if __name__ == '__main__':
  164. #app.run()
  165. poobrains.app.main()