main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from fastapi import FastAPI, Form, Request
  2. from fastapi.encoders import jsonable_encoder
  3. from fastapi.middleware.cors import CORSMiddleware
  4. import dataset
  5. import pandas as pd
  6. from starlette.responses import FileResponse, StreamingResponse
  7. from fastapi.responses import HTMLResponse
  8. from fastapi.staticfiles import StaticFiles
  9. import io
  10. from starlette.responses import RedirectResponse
  11. import codecs
  12. import random
  13. from starlette.status import HTTP_302_FOUND,HTTP_303_SEE_OTHER
  14. app = FastAPI()
  15. app.add_middleware(
  16. CORSMiddleware,
  17. allow_origins=['*'],
  18. allow_credentials=True,
  19. allow_methods=["*"],
  20. allow_headers=["*"],
  21. )
  22. def get_db():
  23. db = dataset.connect('mysql://choozmo:pAssw0rd@139.162.121.30:33306/hhh?charset=utf8mb4')
  24. step_questions_table = db['step_questions']
  25. return step_questions_table
  26. @app.post("/step_questions/submit", response_class=RedirectResponse)
  27. async def submit(request: Request):
  28. form_data = await request.form()
  29. # form_data = request.form()
  30. result = {
  31. 'sex': form_data.get('q1'),
  32. 'phone': form_data.get('q2'),
  33. 'email': form_data.get('q3'),
  34. 'building_case_name': form_data.get('q4'),
  35. 'building_case_type': form_data.get('q5'),
  36. 'decoration_style': ','.join(form_data.getlist('q6')),
  37. 'decoration_budget': ','.join(form_data.getlist('q7')),
  38. 'decoration_size': ','.join(form_data.getlist('q8')),
  39. }
  40. print(result)
  41. get_db().insert(result)
  42. # fr=codecs.open('static/index_complete.html','r','utf-8')
  43. # lines=fr.read()
  44. # fr.close()
  45. # response = RedirectResponse(url='/a1/index_complete.html')
  46. # lines="""<html><body onload="document.location.replace( '/a1/index_complete.html?par={0}');">
  47. # </body></html>"""
  48. # return RedirectResponse(url='/a1/index_complete.html')
  49. # return HTMLResponse(content=lines.format(str(random.randint(100000,999999))), status_code=200)
  50. return RedirectResponse(url="/a1/index_complete.html", status_code=HTTP_302_FOUND)
  51. # return result
  52. app.mount("/a1", StaticFiles(directory="static"), name="static")
  53. @app.get("/a1")
  54. async def redirect():
  55. # url = app.url_path_for("/a1/index.html")
  56. response = RedirectResponse(url='/a1/index.html')
  57. return response
  58. #@app.get("/a1", response_class=HTMLResponse)
  59. #@app.get("/a1", response_class=HTMLResponse)
  60. #async def read_items():
  61. # return """
  62. # <html>
  63. # <head>
  64. # <title>Some HTML in here</title>
  65. # </head>
  66. # <body>
  67. # <h1>Look ma! HTML!</h1>
  68. # </body>
  69. # </html>
  70. # """
  71. @app.get("/step_questions")
  72. async def get_step_question(request: Request):
  73. db_data = list(get_db().find())
  74. df = pd.DataFrame(db_data, columns=db_data[0].keys())
  75. stream = io.StringIO()
  76. df.to_csv(stream, index = False)
  77. # response = StreamingResponse(data_frame.to_csv(index=False), media_type="text/csv")
  78. response = StreamingResponse(iter([stream.getvalue()]), media_type="text/csv")
  79. response.headers["Content-Disposition"] = "attachment; filename=export.csv"
  80. return response