時間:2023-05-30 18:24:02 | 來源:網(wǎng)站運(yùn)營
時間:2023-05-30 18:24:02 來源:網(wǎng)站運(yùn)營
Python Web實(shí)戰(zhàn):Flask + Vue 開發(fā)一個漂亮的詞云網(wǎng)站:.├── backend│ ├── app│ └── venv└── frontend ├── README.md ├── build ├── config ├── dist ├── index.html ├── node_modules ├── package-lock.json ├── package.json ├── src └── static
再來看一下目前代碼的運(yùn)行效果:$ npm install -g vue-cli
$ mkdir word-cloud$ cd word-cloud/
創(chuàng)建項(xiàng)目$ vue init webpack frontend
執(zhí)行完上面的命令后,會讓你設(shè)置項(xiàng)目的基本信息,我的配置如下:$ cd frontend$ npm run dev
執(zhí)行完后會在控制臺提示Your application is running here: http://localhost:8080
說明我們現(xiàn)在已經(jīng)可以跑起來了,可以訪問一下http://localhost:8080
,如下:.├── README.md├── build├── config├── index.html├── node_modules├── package-lock.json├── package.json├── src└── static
$ npm i element-ui -S
使用插件/src/main.js
中導(dǎo)入ElementUIimport ElementUI from'element-ui'import'element-ui/lib/theme-chalk/index.css'
最后使用Vue.use(ElementUI)
$ npm install --save axios
同樣在/src/main.js
導(dǎo)入axiosimport axios from 'axios'
注冊axiosVue.prototype.axios = axios
之后我們就可以使用 axios 發(fā)送請求了。App.vue
,把我們不需要的 logo 刪掉。<template> <div id="app"> <!-- <img src="./assets/logo.png"> --> <router-view/> </div></template>
新建WordCloud.vue
,這就是我們的主要頁面。一個標(biāo)題,一個輸入框,兩個按鈕。<template> <div> <h2>小詞云</h2> <div id="word-text-area"> <el-input type="textarea" :rows="10" placeholder="請輸入內(nèi)容" v-model="textarea"> </el-input> <div id="word-img"> <el-image :src="'data:image/png;base64,'+pic" :fit="fit"> <div slot="error" class="image-slot"> <i class="el-icon-picture-outline"></i> </div> </el-image> </div> <div id="word-operation"> <el-row> <el-button type="primary" @click="onSubmit" round>生成詞云</el-button> <el-button type="success" @click="onDownload" round>下載圖片</el-button> </el-row> </div> </div> </div></template>
實(shí)現(xiàn)點(diǎn)擊事件并發(fā)送請求<script> exportdefault { name: 'wordcloud', data() { return { textarea: '', pic: "", pageTitle: 'Flask Vue Word Cloud', } }, methods: { onSubmit() { var param = { "word": this.textarea } this.axios.post("/word/cloud/generate", param).then( res => { this.pic = res.data console.log(res.data) } ).catch(res => { console.log(res.data.res) }) }, onDownload() { const imgUrl = 'data:image/png;base64,' + this.pic const a = document.createElement('a') a.href = imgUrl a.setAttribute('download', 'word-cloud') a.click() } } }</script>
最后在src/router
中找到index.js
修改一下路由。export default new Router({ routes: [{ path: '/', name: 'index', component: WordCloud }]})
打包資源$ npm run build
執(zhí)行完成后會將資源打包到dist
目錄。brew install python3
由于我之前已經(jīng)安裝過了,執(zhí)行完成之后出現(xiàn)警告,按照提示操作Warning: python 3.7.4_1 is already installed, it's just not linked You can usebrew link python
to link this version.
Linking /usr/local/Cellar/python/3.7.4_1... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks
再次出現(xiàn)錯誤,沒有權(quán)限參考處理:http://stackoverflow.com/questions/2…
sudo chown -R $USER:admin /usr/local
再次執(zhí)行brew link python
Linking /usr/local/Cellar/python/3.7.4_1... 1 symlinks created
錯誤解決,執(zhí)行 python3 可以正確顯示版本號。$ python3Python 3.7.4 (default, Sep 7 2019, 18:27:02)[Clang 10.0.1 (clang-1001.0.46.4)] on darwinType "help", "copyright", "credits" or "license" for more information.
$ mkdir backend$ cd backend/
創(chuàng)建虛擬環(huán)境python3 -m venv venv
激活虛擬環(huán)境source venv/bin/activate
關(guān)閉虛擬環(huán)境的命令如下deactivate
pip install flask
如果沒有報(bào)錯,那就就安裝成果了。pip install wordcloud
__init__.py
中修改python默認(rèn)html和靜態(tài)資源目錄,這個資源就是我們上面在前端開發(fā)中通過npm run build
生成的資源目錄。app = Flask(__name__, template_folder="../../frontend/dist", static_folder="../../frontend/dist/static")
修改完成之后再啟動 Flask,訪問的就是 vue 的頁面了。routes.py
里面的代碼,就是主頁面和生成詞云的接口。# 真正調(diào)用詞云庫生成圖片def get_word_cloud(text): # font = "./SimHei.ttf" # pil_img = WordCloud(width=500, height=500, font_path=font).generate(text=text).to_image() pil_img = WordCloud(width=800, height=300, background_color="white").generate(text=text).to_image() img = io.BytesIO() pil_img.save(img, "PNG") img.seek(0) img_base64 = base64.b64encode(img.getvalue()).decode() return img_base64# 主頁面@app.route('/')@app.route('/index')def index(): return render_template('index.html')# 生成詞云圖片接口,以base64格式返回@app.route('/word/cloud/generate', methods=["POST"])def cloud(): text = request.json.get("word") res = get_word_cloud(text) return res
最后執(zhí)行flask run
就可以跑起來了關(guān)鍵詞:漂亮,實(shí)戰(zhàn)
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。