from pydoc import HTMLDoc from fastapi import FastAPI import dataset import sys import os import time from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from pydantic import BaseModel from fastapi import FastAPI, Form import subprocess import suggests from typing import Optional import networkx as nx import pyvis import time from pyvis.network import Network import pickle import logging import threading import time import random import string from fastapi.responses import HTMLResponse from fastapi.responses import RedirectResponse def id_generator(size=6, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) app = FastAPI() origins = ["*"] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/gtrends?charset=utf8mb4') app.mount("/web", StaticFiles(directory="static"), name="static") def thread_function(kw): global db print(kw) G = nx.Graph() for k in kw: s = suggests.suggests.get_suggests(k, source='google') for sg in s['suggests']: G.add_edge(k,sg,weight=1) print(sg) time.sleep(1) s2 = suggests.suggests.get_suggests(k, source='google') for elmt in s2['suggests']: G.add_edge(sg,elmt,weight=1) G.remove_nodes_from(list(nx.isolates(G))) G.remove_edges_from( list(nx.selfloop_edges(G))) # pickle.dump( G, open( "gs2.p", "wb" ) ) pyG = Network(height="750px", width="100%",bgcolor="#333333",font_color="white") pyG.from_nx(G) id=id_generator() db['gen_graph'].insert({'filename':str(id),'kw':str(kw)}) # pyG.save_graph('gstest') # pyG.show('static/gs/'+str(id)+'.html') pyG.save_graph('static/gs/'+str(id)+'.html') @app.get("/tree_list/",response_class=HTMLResponse) async def tree_list(): # global db db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/gtrends?charset=utf8mb4') html="

清單


請一分鐘後refresh

" html+="" cursor=db.query('select filename,kw from gen_graph order by id desc') cnt=0 for c in cursor: html+="" html+="" cnt+=1 if cnt > 10: break html+="
"+c['kw']+""+c['filename']+"
" return html #response_class=RedirectResponse @app.post("/gen_tree/",response_class=HTMLResponse) async def func_expand(kw: str = Form(...),kw2:Optional[str] = Form(None),kw3:Optional[str] = Form(None),kw4:Optional[str] = Form(None) ): kwlst=[] if len(kw)>1: kwlst.append(kw) if kw2 is not None: kwlst.append(kw2) if kw3 is not None: kwlst.append(kw3) if kw4 is not None: kwlst.append(kw4) x = threading.Thread(target=thread_function, args=(kwlst,)) x.start() # return "ok" return RedirectResponse(url="/tree_list",status_code=302) # return HTMLResponse('')