1
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?

Eelを使ってチャットアプリを作った話

Last updated at Posted at 2025-02-15

はじめに

こんにちは!
Saku0512です。

今回はEelを使ってチャットアプリを作った話をします。

そもそもEelってなんだ?

PythonのEelは、Pythonでデスクトップアプリを簡単に作成できるライブラリです。
バックエンドをPythonで処理し、フロントエンドをHTML/CSS/JavaScriptで構築できます。

特徴

  • バックエンドをPythonで処理し、フロントエンドをHTML/CSS/JavaScriptで構築できる
  • Pythonの関数をJavaScriptから呼び出すことや、JavaScriptの関数をPythonから呼び出すことができる
  • Webサーバーを立てて、ローカルで動作する

インストール

pip install eel

サンプルコード

import eel

# フロントエンドのHTMLフォルダを指定
eel.init('web')

@eel.expose  # Python関数をJavaScriptから呼び出せるようにする
def say_hello(name):
    return f"Hello, {name}!"

# アプリを起動
eel.start('index.html', size=(400, 300))
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Eelアプリ</title>
</head>
<body>
    <h1>EelでPythonとJSを連携</h1>
    <button onclick="callPython()">Pythonを呼ぶ</button>
    <p id="output"></p>

    <script type="text/javascript">
        function callPython() {
            eel.say_hello("Eelユーザー")(function(response) {
                document.getElementById("output").innerText = response;
            });
        }
    </script>
</body>
</html>

ディレクトリ構造

.
├ main.py
└ web/
  └ index.html

実行方法

python main.py

仕組み

  1. eel.init('web')/webフォルダ内のファイルを読み込む
  2. eel.start('index.html', size=(400, 300))でアプリを起動
  3. @eel.exposeでPython関数をJavaScriptから呼び出せるようにする
  4. eel.say_hello("Eelユーザー")でPython関数を呼び出し、結果をJavaScriptに返す

詳細は公式ドキュメントを参照してください。

作ったアプリの解説

Sample_MAIKE.png

概要

Open AI APIを用いてチャット機能を実装し、botuiを用いてチャット画面を作成しました。
ソースコードはGitHubに公開しています。

実装した機能

  • ユーザー登録・ログイン
    1. MySQLを用いてユーザー登録・ログインを実装。
    2. redisを用いてセッション管理を実装。
  • チャット
    1. Eelを用いてjsからpythonに処理を移す。
    2. Open AI APIを用いてチャット機能を実装。
    3. APIから返ってきた文字列を整形してhtmlコードにする。
    4. botuiを用いてチャット画面を作成。
  • 音声入力
    • Web Speech APIを用いて音声入力を実装。
  • ソースコード実行
    • paiza.ioを用いてソースコード実行。
    • C, C#, C++, Python, JavaScript, Java, Ruby, PHPに対応。
1
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
1
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?