Hey guys,

I'm trying to add a webhook to a project but it doesn't work. it always gives an Internal server error. I tried it locally but that worked. Also I tried just returning the request.json and manually changing the values to a dataframe. The site is connected to the web and it receives the json file.

The error is(flask): AttributeError: 'dict' object has no attribute 'to_html'

code from QC:

  1. data = {
  2. 'symbol':self.aapl.Symbol.Value,
  3. 'time':self.Time,
  4. 'order Filled': random.randint(1000, 1001),
  5. 'order Sold': random.randint(1003, 1007),
  6. 'quantity': 1,
  7. 'profit': random.randint(1003, 1007)
  8. }
  9. if self.orders_dataframe is None:
  10. self.orders_dataframe = pd.DataFrame(data=data,index=[0])
  11. else:
  12. self.orders_dataframe = self.orders_dataframe.append(data, ignore_index=True)
  13. json_df = self.orders_dataframe.to_json(date_unit='ns')
  14. self.Notify.Web('https://startfrom0sen.pythonanywhere.com/webhook', json_df)

code on PythonAnywhere:

  1. from flask import Flask, request, Response, render_template, abort
  2. import pandas as pd
  3. from threading import Thread
  4. import json
  5. df = None
  6. app = Flask(__name__,template_folder='templates')
  7. counter = 0
  8. ds = None
  9. @app.route("/webhook", methods=["POST"])
  10. def webhook():
  11. global df
  12. if request.method == 'POST':
  13. counter=+ 1
  14. print(request.json)
  15. if df is None:
  16. df = request.json
  17. df = pd.read_json(df)
  18. ds = df
  19. df = df.iloc[:, [4,5,3,2,0,1]]
  20. df['time'] = pd.to_datetime(df['time'])
  21. else:
  22. js_df = request.json
  23. js_df = pd.read_json(js_df)
  24. js_df = js_df.iloc[:, [4,5,3,2,0,1]]
  25. js_df['time'] = pd.to_datetime(js_df['time'])
  26. df = df.append(js_df)
  27. return df
  28. return "success", 200
  29. else:
  30. abort(400)
  31. @app.route("/")
  32. def main():
  33. if df is None:
  34. return str(counter)
  35. else:
  36. return str(counter), df.to_html()
  37. if __name__ == '__main__':
  38. app.run()
+ Expand

Author

Nico Xenox

January 2023