Dash Speed Test

📡Dash × PlotlyでリアルタイムスピードテストWebアプリを作る完全ガイド【Python】

2025-07-11

✅ はじめに

Python × Dash(Plotly公式)を使って、Google スピードテストのようなリアルタイム Web ダッシュボードを作る方法

🔧 使う技術・ツール一覧

ツール用途
Python開発言語
Dash(Plotly)Webアプリ化&グラフ表示
speedtest-cliインターネット速度測定
Plotly Graph Objectsゲージメーターの表示

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

pip install dash dash-bootstrap-components speedtest-cli

🧐 実行される処理の流れ(概要)

  1. speedtest-cli を使ってネットワーク速度を測定
  2. Plotlygo.Indicator でゲージメーターを表示
  3. Dashdcc.Interval コンポーネントで 60秒ごとに自動更新

✅ コード全文

import dash
from dash import dcc, html
from dash.dependencies import Output, Input
import plotly.graph_objects as go
import speedtest

# Dash アプリの初期化
app = dash.Dash(__name__)
app.title = "Speed Test Dashboard"

# スピードテスト実行関数
def run_speed_test():
    try:
        st = speedtest.Speedtest()
        st.get_best_server()
        download = st.download() / 1_000_000  # Mbps
        upload = st.upload() / 1_000_000
        ping = st.results.ping
        return round(download, 2), round(upload, 2), round(ping, 2)
    except Exception as e:
        print("速度測定中にエラーが発生しました:", e)
        return 0, 0, 0

# ゲージグラフの生成関数
def create_gauge(title, value, color):
    return go.Figure(go.Indicator(
        mode="gauge+number",
        value=value,
        title={'text': title},
        gauge={
            'axis': {'range': [0, 200]},
            'bar': {'color': color},
            'steps': [
                {'range': [0, 50], 'color': "#f2f2f2"},
                {'range': [50, 100], 'color': "#d9f2ff"},
                {'range': [100, 200], 'color': "#a6dfff"},
            ]
        }
    ))

# Dash レイアウト構成
app.layout = html.Div([
    html.H1("リアルタイム スピードテスト", style={"textAlign": "center"}),

    html.Div([
        dcc.Graph(id="download-gauge", style={"display": "inline-block", "width": "45%"}),
        dcc.Graph(id="upload-gauge", style={"display": "inline-block", "width": "45%"})
    ], style={"textAlign": "center"}),

    html.H3(id="ping-display", style={"textAlign": "center", "marginTop": "20px"}),

    dcc.Interval(id="interval", interval=60 * 1000, n_intervals=0)  # 60秒ごとに更新
])

# コールバック関数:リアルタイム更新処理
@app.callback(
    [Output("download-gauge", "figure"),
     Output("upload-gauge", "figure"),
     Output("ping-display", "children")],
    [Input("interval", "n_intervals")]
)
def update_speed(n):
    download, upload, ping = run_speed_test()
    return (
        create_gauge("Download Speed (Mbps)", download, "blue"),
        create_gauge("Upload Speed (Mbps)", upload, "green"),
        f"Ping: {ping} ms"
    )

# ✅ 注意:Dash v3.0 以降は `app.run()` を使う!
if __name__ == '__main__':
    app.run(debug=True)  # ✅ ← これが正解!
        
    

⚠️ よくあるエラーと対処法

❌ ObsoleteAttributeException: app.run_server has been replaced by app.run

🖥️ 実行方法

python speed_test_dash.py

ブラウザで http://127.0.0.1:8050/ を開くだけです

🔗 関連リンク


Pythonのデコレータ(@記号)を初心者向けに解説!

pygameの機能とインストール方法

PythonでTOTP・HOTPを実装する方法|ワンタイムパスワード

Python ブラウザ指定し自動で開く!webbrowserモジュール完全ガイド

Pythonでmoricons.dllのアイコンをPNGとして抽出する方法

【Python音声再生】playsoundではなくpygameを選ぶ理由とは?

Pythonのtqdmライブラリで簡単プログレスバーを実装する方法

📡Dash × PlotlyでリアルタイムスピードテストWebアプリを作る

【Python】pyttsx3のインストール方法と使い方|音声合成で日本語を喋らせる&音声ファイルに保存

【Python入門】pydubで簡単なリズムを作ってmp3保存する方法|audioopエラー対策

pydubで複数のmp3をミックスして保存する方法|音楽合成を簡単に実現

MBTI診断 | あなたの性格タイプを分析

PDFファイル結合|パスワード設定ツール