0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

生成AIチャットを表示する簡易ホームページ

0
Last updated at Posted at 2026-11-19

生成AIチャットを表示する簡易ホームページを Python、Flask、Jinja2 で作成しました。昔は JavaScript、Express、Electron(Chromium ブラウザを内蔵)で作成していたように思います。
ページは Dify(または、他のLLMツール)のチャット機能を呼び出します。

フォルダ・ファイル構成

flask_app/
├── app.py  アプリ本体
└── templates/
    ├── home.html  トップページ
    ├── detail.html  詳細ページ
    ├── image1.png  右上に表示される画像
    ├── image2.png  生成AIチャットの実行イメージ
    └── page-minimum.css  ウェブページの装飾(一部)

アプリ作成

必要なライブラリをインストール

Ubuntu
pip install flask

Flask で、HTML を表示します。

app.py
from flask import Flask, render_template, send_from_directory
from datetime import datetime
from zoneinfo import ZoneInfo

app = Flask(__name__)

@app.route("/")
def index():
    now = datetime.now(ZoneInfo("Asia/Tokyo"))
    current_datetime = (
        f"{now.year}年{now.month}月{now.day}日"
        f"{now.hour}時{now.minute}分"
    )
    return render_template("home.html", current_datetime=current_datetime, area="福島市笹谷", nearest_station = "桜水駅")

@app.route("/detail")
def detail():
    return render_template("detail.html")

@app.route("/image1.png")
def image1():
    return send_from_directory("templates", "image1.png")

@app.route("/image2.png")
def image2():
    return send_from_directory("templates", "image2.png")

@app.route("/page-minimum.css")
def page_minimum_css():
    return send_from_directory("templates", "page-minimum.css")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

トップページになります。
末尾の少し手前の「iframe.src = "https://..."」が Dify のチャット機能のアドレスなので、必要に応じて、変更します。

templates/home.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>じぶんのちいきの生成AI</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.15/tailwind.min.css">
<link rel="stylesheet" href="/page-minimum.css">
<style>
a {
    color: #0d6efd;
    text-decoration: none;
}

a:hover {
    color: #0a58ca;
}
</style>
</head>
<body>

<div class="container">

<!-- Introduction -->
<br/>
<div class="row align-items-center">
<div class="col-lg-8">
<br/>
<br/>
<h2>じぶんのちいきの生成AI</h2>
<br/>
<br/>
住んでいる地域の情報をもとに、日々の暮らしの分からないことに答えてくれる、自分用のAIです。特定の資料などを前提としない一般的な質問は、ChatGPT や Gemini に問い合わせます。<br/>
<br/>
</div>
<div class="col-lg-4 text-center">
<img alt="AI担当" src="/image1.png" style="width:280px; height:auto;"/>
</div>
</div>

<br/>

<!-- Main -->
<p>地域:</p>
<div class="ms-4">
<p><a href="/detail#福島県福島市">福島県福島市</a></p>
</div>

<br/>

<p>適応領域:</p>
<div class="ms-4">
<p>ごみ収集</p>
<p>交通</p>
</div>

<br/>
<br/>

<p>問い合わせを行う:</p>

<p class="mb-1">□ ごみ収集</p>
<div class="ms-4" style="width:80%; overflow-wrap:anywhere;">
<font color="blue">{{ area }}に住んでいます。</font>燃えるごみは何曜日に出せばよいですか?<br/>
<font color="blue">{{ area }}に住んでいます。</font>ごみの分別について説明してください。<br/>
<font color="blue">{{ area }}に住んでいます。現在日時は{{ current_datetime }}です。</font>次に燃えるごみを出す日は、何月何日の何曜日ですか?<br/>
<br/>
<font color="red">※ 2つの質問を同時にしない(同じスレッドで、1つずつ質問する)</font><br/>
<font color="blue">{{ area }}に住んでいます。</font>使用済みの乾電池は、どのごみに分類されますか?<br/>
<font color="blue">現在日時は{{ current_datetime }}です。</font>次にそのごみを出す日は、何月何日の何曜日ですか?<br/>
</div>

<br/>

<p class="mt-2 mb-1">□ 交通</p>
<div class="ms-4" style="width:80%; overflow-wrap:anywhere;">
<font color="blue">現在日時は{{ current_datetime }}です。家の最寄り駅は{{ nearest_station}}です。</font>飯坂線で福島駅に行くために、現在時刻から一番早く乗れる電車は何時ですか?2番目も教えてください。  <font color="red">※ AI は現在時刻を推測することができない</font><br/>
伊達郡桑折町に住んでいます。家にタクシーを呼びたいです。どこに電話すればよいですか?<br/>
</div>

<br/>
<br/>
<br/>

生成AIチャットの実行イメージ:<br/>
<br/>
<div class="ms-4">
<a href="/image2.png" style="display: inline-block;" target="_blank"><img alt="実行イメージ" src="/image2.png" width="400px"/></a>
</div>

<br/>
<br/>
<br/>

<!-- チャットボタン -->
<div id="chat-widget-button">🤖</div>

<!-- チャットウィンドウ -->
<div id="chat-widget-window">
    <iframe
        id="chat-widget-iframe"
        title="生成AIチャット"
        src=""
        allow="microphone">
    </iframe>
</div>

<style>
#chat-widget-button{
    position:fixed;
    right:180px;
    bottom:90px;
    width:50px;
    height:50px;
    background:#007bff;
    color:white;
    border-radius:50%;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:26px;
    cursor:pointer;
    z-index:9999;
}

#chat-widget-window{
    position:fixed;
    right:20px;
    bottom:150px;

    width:350px;
    height:420px;

    display:none;
    overflow:hidden;
    background:white;
    border:1px solid #ddd;
    border-radius:10px;
    box-shadow:0 4px 15px rgba(0,0,0,0.2);
    z-index:9999;
}

#chat-widget-window iframe{
    width:100%;
    height:100%;
    border:none;
}

@media (min-width: 992px){
    #chat-widget-window{
        height:550px;
    }
}
</style>

<script>
document.addEventListener("DOMContentLoaded", function() {

    const button = document.getElementById("chat-widget-button");
    const window = document.getElementById("chat-widget-window");
    const iframe = document.getElementById("chat-widget-iframe");

    button.addEventListener("click", function() {

        if (window.style.display === "block") {
            // Close chat window
            window.style.display = "none";
        } else {
            // Load chatbot only once
            if (!iframe.getAttribute("src")) {
                iframe.src = "https://bunkyo-ai.homelinux.com/chat/H5j2Laj8oOx5XX2X";
            }

            // Open chat window
            window.style.display = "block";
        }

    });

});
</script>

</div>

<!-- Footer -->
<footer class="bg-gray-900 text-white py-4">
<div class="container text-center"> © 2026 じぶんのちいきの生成AI.  All Rights Reserved.</div>
</footer>
</body>
</html>

該当リンクをクリックすると詳細ページを表示します。本件では RAG に登録した資料の一覧を表示します。

templates/detail.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>じぶんのちいきの生成AI</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.15/tailwind.min.css">
<link rel="stylesheet" href="/page-minimum.css">
<style>
a {
    color: #0d6efd;
    text-decoration: none;
}

a:hover {
    color: #0a58ca;
}
</style>
</head>
<body>

<div class="container">

<!-- Main -->
<br/>
<a href="/">[ホーム]</a><br/>
<br/>
<br/>
<a name="福島県福島市"></a><h4 style="font-size: 1.5rem; font-weight: 500;">福島県福島市</h4>
<hr/>
<br/>
福島県福島市「資源にできない埋めるごみ(埋めるごみ)」2026年1月16日<br/>
福島県福島市「資源にできない燃やすごみ(燃やすごみ)」2026年3月11日<br/>
福島県福島市「資源物(リサイクルできるもの)」2026年3月16日<br/>
福島県福島市「令和8年度 ごみ・資源物収集カレンダー」2026年9月12日<br/>
<br/>
福島県福島市「飯坂電車 時刻表」2026年9月4日<br/>
福島県福島市「阿武隈急行 時刻表」2026年9月6日<br/>
福島県福島市「あづまタクシー」2026年9月15日<br/>
<br/>
※登録日:元の資料に改正日が記載されていれば、それを記述しています<br/>
<br/>
<br/>

<!-- チャットボタン -->
<div id="chat-widget-button">🤖</div>

<!-- チャットウィンドウ -->
<div id="chat-widget-window">
    <iframe
        id="chat-widget-iframe"
        title="生成AIチャット"
        src=""
        allow="microphone">
    </iframe>
</div>

<style>
#chat-widget-button{
    position:fixed;
    right:180px;
    bottom:90px;
    width:50px;
    height:50px;
    background:#007bff;
    color:white;
    border-radius:50%;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:26px;
    cursor:pointer;
    z-index:9999;
}

#chat-widget-window{
    position:fixed;
    right:20px;
    bottom:150px;

    width:350px;
    height:420px;

    display:none;
    overflow:hidden;
    background:white;
    border:1px solid #ddd;
    border-radius:10px;
    box-shadow:0 4px 15px rgba(0,0,0,0.2);
    z-index:9999;
}

#chat-widget-window iframe{
    width:100%;
    height:100%;
    border:none;
}

@media (min-width: 992px){
    #chat-widget-window{
        height:550px;
    }
}
</style>

<script>
document.addEventListener("DOMContentLoaded", function() {

    const button = document.getElementById("chat-widget-button");
    const window = document.getElementById("chat-widget-window");
    const iframe = document.getElementById("chat-widget-iframe");

    button.addEventListener("click", function() {

        if (window.style.display === "block") {
            // Close chat window
            window.style.display = "none";
        } else {
            // Load chatbot only once
            if (!iframe.getAttribute("src")) {
                iframe.src = "https://bunkyo-ai.homelinux.com/chat/H5j2Laj8oOx5XX2X";
            }

            // Open chat window
            window.style.display = "block";
        }

    });

});
</script>

</div>

<!-- Footer -->
<footer class="bg-gray-900 text-white py-4">
<div class="container text-center"> © 2026 じぶんのちいきの生成AI.  All Rights Reserved.</div>
</footer>
</body>
</html>

ウェブページの見た目をよくするための CSS です。他のウェブページから必要な部分を持って来ました。

templates/page-minimum.css
/* Minimal styles for the supplied home.html, replacing app-BsyzhWf_.css.
   Keep the existing Tailwind CSS link for footer utilities. */
@import url('https://fonts.bunny.net/css?family=Nunito');
:root { font-family: Nunito, sans-serif; }
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: Nunito, sans-serif; font-size: .9rem; font-weight: 400; line-height: 1.6; color: #212529; background-color: #f8fafc; }
h2 { margin: 0 0 .5rem; font-weight: 500; line-height: 1.2; font-size: calc(1.305rem + .66vw); }
p { margin: 0 0 1rem; }
img { vertical-align: middle; }
.container { width: 100%; padding-right: .75rem; padding-left: .75rem; margin-right: auto; margin-left: auto; }
.row { display: flex; flex-wrap: wrap; margin-right: -.75rem; margin-left: -.75rem; }
.row > * { flex-shrink: 0; width: 100%; max-width: 100%; padding-right: .75rem; padding-left: .75rem; }
.align-items-center { align-items: center !important; }
.text-center { text-align: center !important; }
.ms-4 { margin-left: 1.5rem !important; }
.mb-1 { margin-bottom: .25rem !important; }
.mt-2 { margin-top: .5rem !important; }
@media (min-width: 576px) { .container { max-width: 540px; } }
@media (min-width: 768px) { .container { max-width: 720px; } }
@media (min-width: 992px) {
  .container { max-width: 960px; }
  .col-lg-8 { flex: 0 0 auto; width: 66.666667%; }
  .col-lg-4 { flex: 0 0 auto; width: 33.333333%; }
}
@media (min-width: 1200px) { .container { max-width: 1140px; } h2 { font-size: 1.8rem; } }
@media (min-width: 1400px) { .container { max-width: 1320px; } }

アプリ実行

Ubuntu
python app.py

生成AIチャットの実行イメージ
ブラウザで http://127.0.0.1:5000 を表示すると、ホームページが表示されます。

詳細ページの例

 

以上、

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?