main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from fastapi import FastAPI
  2. from fastapi.staticfiles import StaticFiles
  3. from apis.general_pages.route_homepage import general_pages_router
  4. from core.config import settings
  5. # import pandas as pd
  6. # from fastapi import FastAPI, File, UploadFile, Request
  7. # from fastapi.middleware.cors import CORSMiddleware
  8. # import dataset
  9. # from functools import reduce
  10. # from fastapi.staticfiles import StaticFiles
  11. # from fastapi.templating import Jinja2Templates
  12. # from fastapi.responses import HTMLResponse
  13. # app = FastAPI()
  14. # app.add_middleware(
  15. # CORSMiddleware,
  16. # allow_origins=['*'],
  17. # allow_credentials=True,
  18. # allow_methods=["*"],
  19. # allow_headers=["*"],
  20. # )
  21. # templates = Jinja2Templates(directory="templates")
  22. #
  23. # app.mount("/static", StaticFiles(directory="static"), name="static")
  24. # @app.get("/")
  25. # def hello_api():
  26. # return {"msg":"Hello API"}
  27. def include_router(app):
  28. app.include_router(general_pages_router)
  29. def configure_static(app):
  30. app.mount("/static", StaticFiles(directory="static"), name="static")
  31. def start_application():
  32. app = FastAPI(title=settings.PROJECT_NAME,version=settings.PROJECT_VERSION)
  33. include_router(app)
  34. return app
  35. app = start_application()
  36. # @app.post("/write/")
  37. # async def writecsv(file: UploadFile = File(...)):
  38. #
  39. # # read csv
  40. # repls = (' Stats ', '_'), (' at ', '_'), ('-', '_'), \
  41. # (' ', ''), ('.csv', '')
  42. # filename = file.filename
  43. # filename = reduce(lambda a, kv: a.replace(*kv), repls, filename)
  44. #
  45. # data = file.file
  46. # df = pd.read_csv(data, sep=",",
  47. # skiprows=0, na_values='NULL')
  48. #
  49. # # db connect
  50. # # db = dataset.connect(
  51. # # 'mysql://choozmo:pAssw0rd@db.ptt.cx:3306/keywordweb?charset=utf8mb4'
  52. # # )
  53. # db = dataset.connect(
  54. # 'mysql://root:pAssw0rd@localhost:3306/keywordweb?charset=utf8mb4'
  55. # )
  56. # sql = "CREATE TABLE IF NOT EXISTS "\
  57. # + filename +\
  58. # "(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, " \
  59. # "keyword VARCHAR(100), " \
  60. # "currency VARCHAR(100), avg_monthly_searches VARCHAR(100), " \
  61. # "three_month_change VARCHAR(100), " \
  62. # "yearly_change VARCHAR(100), competition VARCHAR(100), " \
  63. # "competition_indexed VARCHAR(100), " \
  64. # "top_page_bid_low_range VARCHAR(100), " \
  65. # "top_page_bid_hi_range VARCHAR(100)" \
  66. # ");"
  67. #
  68. # db.query(sql)
  69. #
  70. # # write to db
  71. # table = db[filename]
  72. # lines = df.shape
  73. # i = 0
  74. #
  75. # rows = lines[0]
  76. #
  77. # for i in range(rows):
  78. # row1 = df.iloc[i]
  79. # k = row1[0]
  80. # c = row1[1]
  81. # a = row1[2]
  82. # t = row1[3]
  83. # y = row1[4]
  84. # c1 = row1[5]
  85. # c2 = row1[6]
  86. # t1 = row1[7]
  87. # t2 = row1[8]
  88. #
  89. # dbdata = dict(keyword=k, currency=c, avg_monthly_searches=a,
  90. # three_month_change=t, yearly_change=y, competition=c1,
  91. # competition_indexed=c2, top_page_bid_low_range=t1,
  92. # top_page_bid_hi_range=t2)
  93. # table.insert(dbdata)
  94. #
  95. # db.close()
  96. #
  97. #
  98. #
  99. # @app.get("/items/{id}", response_class=HTMLResponse)
  100. # async def read_item(request: Request, id: int):
  101. # x = 0
  102. # nums = x + 11
  103. # title = "報表"
  104. # keyword = "關鍵字1"
  105. # return templates.TemplateResponse(
  106. # "item.html",
  107. # {"request": request, "id": id,
  108. # "num": nums, "titles": title, "keyword":keyword
  109. # })
  110. #