added api_bp.py and change in main.py #99
Closed
buya
wants to merge 1 commits from
Bug473-Buya
into master
pull from: Bug473-Buya
merge into: mrlan:master
mrlan:Alpha-snapshot20240618
mrlan:Bug580-Hui
mrlan:Bug565-Hui
mrlan:Bug394-Hui
mrlan:Bug478-Hui
mrlan:Bug501-Hui
mrlan:Bug511-Hui
mrlan:Bug518-Hui
mrlan:Bug563-Hui
mrlan:bug555-fangchen
mrlan:Bug547-FanWenQi-Adapted
mrlan:SPM2023S-QianJunQi
mrlan:Bug536-Jiangwangzhe
mrlan:Bug540-XiongJiaming
mrlan:Bug579-LuKangyang
mrlan:BUG543-JiWenkai
mrlan:Bug574-ChenLingjie2
mrlan:Bug573-PanBinjie
mrlan:Bug570_CaiShuHuang
mrlan:Bug578-ChenChen2
mrlan:Bug578-ChenChen
mrlan:Bug577-JiangXueQin
mrlan:Bug576-XiaBaizhi
mrlan:Bug564-JiangChao
mrlan:Bug572-ZhongYi2
mrlan:Bug533-ZhangXuDong
mrlan:Bug392-LiJie
mrlan:Bug358-LiJie
mrlan:Bug534-WangWeitao
mrlan:Bug569-YuTianshuai
mrlan:Bug563-HuangHaoqi
mrlan:Bug571-TongQi
mrlan:Bug572-ZhongYi
mrlan:Bug566-SunJiawen
mrlan:Bug574-ChenLingjie
mrlan:Bug565-ChenYuhang
mrlan:Bug568-SongHaiyan
mrlan:Bug567-YuZheChen
mrlan:Bug571-TonQi
mrlan:Bug561-LiangZiyue
mrlan:Bug511-Bosh
mrlan:Bug555-chenshiying
mrlan:Bug513-Mayada
mrlan:Bug543-AyaOK
mrlan:Bug518-Mponeja
mrlan:Bug543-Aya
mrlan:Bug547_FanWenQi
mrlan:bug-497Gongzhengcheng
mrlan:Bug500-Badr
mrlan:Bug518-Esther
mrlan:Bug536-QianLetao
mrlan:Alpha-snapshot20230621
mrlan:Bug533-Yuyikai
mrlan:Bug562-Wuyichen
mrlan:Alpha-snapshot20230621OK
mrlan:Bug476-LiMengdie
mrlan:Bug545-HuangHuiLing
mrlan:Bug561-WanZiKun
mrlan:BugTBD-LiSinan
mrlan:master
mrlan:Bug579
mrlan:0618、
mrlan:Bug551-DingZeYu
mrlan:547
mrlan:542
mrlan:Bug528-TangJiao
mrlan:Bug553_LinShan
mrlan:Bug-561
mrlan:fix-vuln
mrlan:Bug476-Yuhuangtao
mrlan:Bug527-ZhouZhifang
mrlan:Alpha-snapshot20230615
mrlan:Bug534-NingShushuang
mrlan:improvment_GuHan
mrlan:Bug393-TanYanMei
mrlan:improvment-NiWeiCong2
mrlan:refactor-huangzirui
mrlan:Bosh
mrlan:Alpha-snapshot20230605
mrlan:refactor-wangyu
mrlan:Alpha-snapshot20230601
mrlan:Alpha-snapshot20230531
mrlan:Bug529-GuHan
mrlan:Bug492-XuHongJian-HuangZirui
mrlan:Alpha-snapshot20230529
mrlan:Refactor_qianjunqi
mrlan:Bug522-HuangZirui
mrlan:Refactor-XunYucan
mrlan:Alpha-snapshot20230525
mrlan:Refactor-HeZhengzheng
mrlan:Alpha-snapshot20230511
mrlan:Bug407-JinHaoLin
mrlan:Alpha-snapshot20230519
mrlan:Bug476-YuHuangtao
mrlan:Bug476-ZhangWeiHao-YuHuangtao
mrlan:Bug473-LuXiaochen
mrlan:Bug493-GongKeCheng
mrlan:Bug488-TangWei
mrlan:SOFTARCH2023S-ZENOVIO
mrlan:Bug532-HuangDan
mrlan:Bug476-ZhangWeiHao-BaoYuelin
mrlan:Bug504-LiJia
mrlan:SPM2023-PR44-YuGaoxiang
mrlan:Bug476-ZhangWeiHao
mrlan:Alpha-snapshot20230507
mrlan:Huangdan
mrlan:Bug509-XieQiuHan-WangZiming-HuangDan
mrlan:Alpha-snapshot20230506
mrlan:improvment-NiWeiCong
mrlan:Bug502-YuGaoXiang
mrlan:bug359-zhangkeli
mrlan:593
mrlan:Alpha-snapshot20230427
mrlan:Alpha-snapshot20230425
mrlan:Bug509-XieQiuHan-WangZiming
mrlan:Alpha-snapshot20230426
mrlan:Alpha
mrlan:Lanhui-update-README2
mrlan:SPM2022F-CONTRIBUTORS-WuWenZhuo
mrlan:Bug490-ChenQiuwei
mrlan:Bug525-Hui
mrlan:englishpal
mrlan:AAALF
mrlan:Bug509-XieQiuHan
mrlan:SPM2022F-CONTRIBUTORS-DingRui
mrlan:SPM2022F-CONTRIBUTORS-jiaojiao
mrlan:Bug521-LiYuFeng-refactor
mrlan:Bug492-XuHongJian
mrlan:SPM2022F-CONTRIBUTORS-XIEQIUHAN
mrlan:SPM2022F-CONTRIBUTORS-XuHongJian
mrlan:509
mrlan:Bug512-RenYu
mrlan:SPM2022F-CONTRIBUTORS-GONGKE
mrlan:Bug521-LiYuFeng
mrlan:SPM2022F-CONTRBUTORS-luofei
mrlan:SPM2022F-CONTRIBUTORS-RUANYUXUAN
mrlan:bug492
mrlan:SPM2022F-CONTRIBUTORS-LINJUNHONG
mrlan:SPM2022F-CONTRIBUTORS-TANGWEI
mrlan:Bug508-CenHaotian
mrlan:SPM2022F-CONTRIBUTORS-GONGKECHENG
mrlan:Bug499-Hui
mrlan:SPM2022F-CONTROIBUTORS-XUHONGJIAN
mrlan:SPM2022F-CONTRBUTORS-TECHLEADNAME
mrlan:Bug505-ZhangYiteng
mrlan:Bug495-LiangLiGang
mrlan:SPM2022F-CONTRIBUTORS-LIJIA
mrlan:SPM2022F-CONTRIBUTORS-CENHAOTIAN
mrlan:SPM2022F-CONTRIBUTORS-ChenQiuwei
mrlan:Bug499-WangZiming
mrlan:Bug487-WuYuhan-Refactor
mrlan:Hui-Build
mrlan:Bug487-WuYuhan
mrlan:Bug412-JiangLetian-Refactor
mrlan:Bug412-JiangLetian
mrlan:Hui-EscapeUserInput
mrlan:Bug422-XuXing
mrlan:Improvement-Stewart
mrlan:Bug400-QiuZhonghui-Refactor
mrlan:Bug477-ChenJingyi
mrlan:Bug400-QiuZhonghui
mrlan:Bug474-RenYinJie
mrlan:Bug344-JiangXueHong
mrlan:Bug428-LouJiCheng
mrlan:SOFTARCH2022S-ZAYID-478
mrlan:Bug392-LuoYu
mrlan:Bug358-TengJiaQian
mrlan:Bug410-DuanJiaJie
mrlan:Bug209-LiuChangYou
mrlan:Bug393-QinYanMei
mrlan:IMPROVE-WangWeiLong
mrlan:Bug391-LiuYiXiu
mrlan:Bug260-Mohanad
mrlan:bug209_yaaqobv2
mrlan:BugFix407-JinHaoLin
mrlan:Bug394-MiaoChenShuo
mrlan:bug209-yaaqob
mrlan:Bug257-MaJiaBin
mrlan:Bug395-Anxiuxiu
mrlan:Deployment
mrlan:ChenTianle-TypoCorrection
mrlan:Hui-SaferSQL
mrlan:BugFix347
mrlan:WangXuan-Highlight-Pronounce
mrlan:Lanhui-Selenium
mrlan:Lanhui-update-README
mrlan:BugFix300
mrlan:Lanhui-IncreaseChanceOfGettingDifficultArticles
mrlan:BugFix293
mrlan:BugFix284
mrlan:Lanhui-flash-messages
mrlan:Lanhui-go-bootstrap
mrlan:Lanhui-build.sh
mrlan:BugFix254-Author-ZhanJianhao
mrlan:Lanhui-add-articles
No reviewers
Labels
Clear labels
No items
No Label
Milestone
Clear milestone
No items
No Milestone
Projects
Clear projects
No project
Assignees
Clear assignees
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.
No due date set.
Dependencies
No dependencies set.
Reference: mrlan/EnglishPal#99
Reference in New Issue
There is no content yet.
Delete Branch "Bug473-Buya"
Deleting a branch is permanent. Although the deleted branch may exist for a short time before cleaning up, in most cases it CANNOT be undone. Continue?
At the beginning of the code, the necessary imports are included. These include the json module for working with JSON data and several modules from Flask, such as Blueprint for creating modular application structures, session for accessing user session data, and jsonify for converting Python objects to JSON responses.
We then creates a Flask blueprint named api_blue. Blueprints allow for organizing related routes and functionality in a modular way. The api_blue blueprint is configured to handle routes under the /api URL prefix.
Next, there is a helper function called helper. This function takes a dictionary-like structure res and a list result. It recursively traverses the dictionary, extracting keys and appending them to the result list. This function is later used to extract keys from a dictionary named s.
The main view function api_bp is defined and associated with the /json/ route within the api_blue blueprint. This function takes the username as a parameter, indicating that it expects this value to be included in the URL.
Within api_bp, the function retrieves the token value from the user's session using session.get("token"). It checks if the retrieved token matches a predefined valid token ("70620F32A9DC965FCCF0447B674AA161"). If the token is valid, the code within the if block is executed.
Within the if block, the function constructs a path to a frequency data pickle file based on the username. The pickle_idea2.load_record function is then used to load the data from the pickle file into a variable named s. This data represents the frequency records for the given username.
The helper function is called with the s dictionary and an initially empty result list. This extracts the keys from the s dictionary and stores them in a list named wordlist.
A dictionary named results is initialized as an empty dictionary. The code then iterates over each word in the wordlist. For each word, it retrieves the corresponding value from the s dictionary (which is a list of frequency data) and assigns the length of that list as the value in the results dictionary. In other words, it calculates the frequency count for each word.
Finally, the results dictionary is returned as a JSON response using the jsonify function. This function converts the Python dictionary into a JSON object and sets the appropriate response headers.
If the token is found to be invalid, the code within the else block is executed. It simply returns an error response as a JSON object with the key "error" set to the value "无效的token" (which means "Invalid token" in Chinese) using the jsonify function.
Better pull request exists. See Pull Request 93.
Pull request closed