如何使用Flask服务NPM软件包? [英] How can I serve NPM packages using Flask?

查看:113
本文介绍了如何使用Flask服务NPM软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型Flask应用,该应用当前从外部服务器上获取jQuery和Highlight.js.我想通过NPM建立这些本地依赖项.

I have a small Flask app which currently sources jQuery and highlight.js from external servers. I'd like to make these local dependencies which I pull in via NPM.

这是什么标准做法?我应该在与statictemplates目录相同的目录中创建package.json文件并将node_modules作为单独的静态目录提供吗

What is the standard practice for this? Should I create package.json file in the same directory as my static and templates directories and serve node_modules as a separate static dir ala this question?

我正在使用pip打包和分发我的应用程序,因此任何解决方案都必须与此兼容.

I'm packaging and distributing my app using pip, so any solution needs to be compatible with that.

推荐答案

转到静态文件夹,然后在其中初始化npm项目.

Go to your static folder and there initialize your npm project.

cd flask_app/static
$ npm init

在安装和保存npm软件包后,您可以像这样为它们提供服务:

after installing and saving npm packages you can serve them like this:

<script src="{{ url_for('static', filename='node_modules/toastr/toastr.js')}}"></script>

信用至: https://codeburst.io/creating-a-full-stack-web-application-with-python-npm-webpack-and-react-8925800503d9

这篇关于如何使用Flask服务NPM软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆