如何在本地主机中使用Python Alone显示HTML [英] How to display HTML using Python Alone in localhost

查看:58
本文介绍了如何在本地主机中使用Python Alone显示HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅使用python显示我的html文件.例如我会python code.py,我将能够在localhost:8080

I am trying to display my html files using python alone. Such as I would python code.py and I would be able to access my html at localhost:8080

html文件是可以相互访问的静态文件.例如,index.html定向到contact.html,它们所有人都访问css文件夹.

html files are static files that access each other. For example, index.html directs to contact.html and all of them access css folder.

如何打开html文件,使其显示在网页上?

How do I open my html files so it displays it on the webpage?

以下是我到目前为止的内容.

below is what I have so far.

html_file = []
with open("index.html", "r") as f:
    for line in f.readlines():
        html_file.append(line)

有没有办法做python code.py,当我访问localhost:8000时,会向我显示代码?我可以访问每个页面.

Is there way to do python code.py and when I access localhost:8000 that would show me the code? and I can access each page.

这是一个示例html文件.

here is an example html file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>title</title>
    <link rel="stylesheet" href="./css/style.css">
</head>
<body>
    <header>
        <img class="resize" src="./pictures/logo.png" alt="logo">
        <nav>
            <ul class="nav-links">
                <li class="active"><a href="./index.html">Home</a></li>
                <li><a href="./contact.html">Contact</a></li>
            </ul>
            <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
            <script type="text/javascript">
                $(document).on('click', 'ul li', function(){
                    $(this).addClass('active').siblings().removeClass('active')
                })
            </script>
        </nav>
    </header>
</body>
</html>

推荐答案

可以使用python Web框架(如FlaskDjango)来做到这一点.在典型的烧瓶场景中,您的代码将如下所示:-

There's way to do so using the python web frameworks like Flask or Django. In a typical flask scenario your code would look like this:-

1)安装烧瓶:-

pip install flask

2)像这样写您的code.py:-

2) Write your code.py like this:-

from flask import Flask, url_for
from flask import render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('hello.html')

3)接下来,创建一个模板folder,在其中放置您命名为hello.html

3) Next create a templates folder inside which put your html file which I have named it to be hello.html

templates > hello.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>title</title>
    <link rel="stylesheet" href="./css/style.css">
</head>
<body>
    <header>
        <img class="resize" src="./pictures/logo.png" alt="logo">
        <nav>
            <ul class="nav-links">
                <li class="active"><a href="./index.html">Home</a></li>
                <li><a href="./contact.html">Contact</a></li>
            </ul>
            <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
            <script type="text/javascript">
                $(document).on('click', 'ul li', function(){
                    $(this).addClass('active').siblings().removeClass('active')
                })
            </script>
        </nav>
    </header>
</body>
</html>

4)您的目录结构如下:-

4) Your directory structure would look like this:-

/code.py
/templates
    /hello.html

5)运行python code.py,您可以在localhost:5000上看到您的页面.

5) Run python code.py and you can see your page on localhost:5000.

这篇关于如何在本地主机中使用Python Alone显示HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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