CJYen 3 years ago
parent
commit
00be2cfbd0
2 changed files with 102 additions and 29 deletions
  1. 40 0
      api/heatmap.py
  2. 62 29
      api/main.py

+ 40 - 0
api/heatmap.py

@@ -0,0 +1,40 @@
+import plotly_express as px
+import pandas as pd
+import plotly
+
+# Read data from excel
+df = pd.read_csv("../docs/keyword.csv")
+keyword = df['Keyword']
+
+# competition = df['Competition (indexed value)']
+bid = df['Top of page bid (low range)']
+search = df['Avg_monthly_searches']
+margin = df['Top of page bid (low range)']
+remark = df['Keyword']
+
+
+# Create chart and store as figure [fig]
+fig = px.treemap(df,
+                 path=[keyword, bid],
+                 values=search,
+                 color=margin,
+                 # color_continuous_scale=['green', 'yellow', 'red'],
+                 color_continuous_scale=px.colors.sequential.Peach,
+                 title='Keyword Overview',
+                 hover_name=remark,
+                 )
+
+fig.update_traces(root_color="lightgrey")
+
+
+# Update/change layout
+fig.update_layout(title_font_size=42,
+                  title_font_family='Arial',
+                  margin=dict(t=50, l=25, r=25, b=25)
+                  )
+
+fig.show()
+
+
+# save chart and export to HTML
+plotly.offline.plot(fig, filename='../chart.html')

+ 62 - 29
api/main.py

@@ -1,40 +1,73 @@
-import plotly_express as px
 import pandas as pd
-import plotly
+from fastapi import FastAPI, File, UploadFile
+from fastapi.middleware.cors import CORSMiddleware
+import dataset
+from functools import reduce
 
-# Read data from excel
-df = pd.read_csv("../docs/keyword.csv")
-keyword = df['Keyword']
+app = FastAPI()
+app.add_middleware(
+    CORSMiddleware,
+    allow_origins=['*'],
+    allow_credentials=True,
+    allow_methods=["*"],
+    allow_headers=["*"],
+)
 
-# competition = df['Competition (indexed value)']
-bid = df['Top of page bid (low range)']
-search = df['Avg_monthly_searches']
-margin = df['Top of page bid (low range)']
-remark = df['Keyword']
 
+@app.post("/write/")
+async def writecsv(file: UploadFile = File(...)):
+    # read csv
+    repls = (' Stats ', '_'), (' at ', '_'), ('-', '_'), \
+            (' ', ''), ('.csv', '')
+    filename = file.filename
+    filename = reduce(lambda a, kv: a.replace(*kv), repls, filename)
 
-# Create chart and store as figure [fig]
-fig = px.treemap(df,
-                 path=[keyword, bid],
-                 values=search,
-                 color=margin,
-                 # color_continuous_scale=['green', 'yellow', 'red'],
-                 color_continuous_scale=px.colors.sequential.Peach,
-                 title='Keyword Overview',
-                 hover_name=remark,
-                 )
+    data = file.file
+    df = pd.read_csv(data, sep=",",
+                     skiprows=0, na_values='NULL')
 
-fig.update_traces(root_color="lightgrey")
+    # db connect
+    db = dataset.connect(
+        'mysql://choozmo:pAssw0rd@db.ptt.cx:3306/keywordweb?charset=utf8mb4'
+    )
 
+    sql = "CREATE TABLE IF NOT EXISTS " \
+          + filename + \
+          "(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, " \
+          "keyword VARCHAR(100), " \
+          "currency VARCHAR(100), avg_monthly_searches VARCHAR(100), " \
+          "three_month_change VARCHAR(100), " \
+          "yearly_change VARCHAR(100), competition VARCHAR(100), " \
+          "competition_indexed VARCHAR(100), " \
+          "top_page_bid_low_range VARCHAR(100), " \
+          "top_page_bid_hi_range VARCHAR(100)" \
+          ");"
 
-# Update/change layout
-fig.update_layout(title_font_size=42,
-                  title_font_family='Arial',
-                  margin=dict(t=50, l=25, r=25, b=25)
-                  )
+    db.query(sql)
 
-fig.show()
+    # write to db
+    table = db[filename]
+    lines = df.shape
+    i = 0
 
+    rows = lines[0]
 
-# save chart and export to HTML
-plotly.offline.plot(fig, filename='../chart.html')
+    for i in range(rows):
+        row1 = df.iloc[i]
+        k = row1[0]
+        c = row1[1]
+        a = row1[2]
+        t = row1[3]
+        y = row1[4]
+        c1 = row1[5]
+        c2 = row1[6]
+        t1 = row1[7]
+        t2 = row1[8]
+
+        dbdata = dict(keyword=k, currency=c, avg_monthly_searches=a,
+                      three_month_change=t, yearly_change=y, competition=c1,
+                      competition_indexed=c2, top_page_bid_low_range=t1,
+                      top_page_bid_hi_range=t2)
+        table.insert(dbdata)
+
+    db.close()