gpt4 book ai didi

python - 如何在本地(Windows)运行Python Web应用程序

转载 作者:行者123 更新时间:2023-11-30 23:32:29 26 4
gpt4 key购买 nike

我刚刚下载了一个使用 Python 编写的 Web 应用程序。我对 Python 完全陌生。在我做了一些研究之后,以下是我所做的

1)安装Python 2.7

2) 安装 pip ( How do I install pip on Windows? )

当我尝试使用此命令运行 python 文件时

Python PATH/test.py

显示

Traceback (most recent call last):
File "PATH\test.py", line 1, in <module>
from flask import Blueprint, flash, request, render_template

Python 代码:

from flask import Blueprint, flash, request, render_template
from steam import vdf
import json

vdfjson = Blueprint("vdfjson", __name__, template_folder="templates")


@vdfjson.route('/', methods=["GET", "POST"])
def index():
response = None
format = "json"
if request.method == "POST":
format = request.form["format"]
data = request.form["data"]

try:
if format == "vdf":
response = json.dumps(
vdf.loads(data),
indent=4
)

elif format == "json":
_response = json.loads(data)
response = vdf.dumps(_response).decode("utf-16")

except ValueError:
flash("ValueError: Your {} may not be valid.".format(format), "danger")
response = "{}" if format == "json" else ""

return render_template("vdfjson.html", response=response, format=format, title="vdfjson")

*这是一个网络应用程序,所以我不确定我是否遵循正确的说明。

我尝试安装flask

pip install flask

我得到以下错误

C:\Python27\Scripts>pip install Flask
Downloading/unpacking Flask
Downloading Flask-0.10.1.tar.gz (544kB): 544kB downloaded
Running setup.py egg_info for package Flask
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'include_package_data'
warnings.warn(msg)
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'zip_safe'
warnings.warn(msg)
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'install_requires'
warnings.warn(msg)
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'test_suite'
warnings.warn(msg)
usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: -c --help [cmd1 cmd2 ...]
or: -c --help-commands
or: -c cmd --help

error: invalid command 'egg_info'
Complete output from command python setup.py egg_info:
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'include_package_data'

warnings.warn(msg)

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option:
'zip_safe'

warnings.warn(msg)

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option:
'install_requires'

warnings.warn(msg)

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option:
'test_suite'

warnings.warn(msg)

usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]

or: -c --help [cmd1 cmd2 ...]

or: -c --help-commands

or: -c cmd --help



error: invalid command 'egg_info'

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in c:\users\sam\appdat
a\local\temp\pip_build_SAM\Flask
Storing complete log in C:\Users\SAM\pip\pip.log

C:\Python27\Scripts>

之后我再次执行

Python PATH/test.py

它没有显示任何错误,但什么也不做。

最佳答案

这不是一个完整的 Flask 应用程序。您需要创建 Flask 实例,然后在其中注册您的蓝图。尝试运行以下代码:

from flask import Blueprint, Flask, flash, request, render_template
from steam import vdf
import json
app = Flask(__name__)


vdfjson = Blueprint("vdfjson", __name__, template_folder="templates")
app.register_blueprint(vdfjson)


@vdfjson.route('/', methods=["GET", "POST"])
def index():
response = None
format = "json"
if request.method == "POST":
format = request.form["format"]
data = request.form["data"]

try:
if format == "vdf":
response = json.dumps(
vdf.loads(data),
indent=4
)

elif format == "json":
_response = json.loads(data)
response = vdf.dumps(_response).decode("utf-16")

except ValueError:
flash("ValueError: Your {} may not be valid.".format(format), "danger")
response = "{}" if format == "json" else ""

return render_template("vdfjson.html", response=response, format=format, title="vdfjson")

if __name__ == '__main__':
app.run()

关于python - 如何在本地(Windows)运行Python Web应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19316014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com