この記事を作った動機

 2025/9/10 から、今日に至るまでの間で OneNote 代替に関して、実装をするなど研究を進めているうちにわかったことなどを簡易的に記録する。

nested dict (python)

 dict 型のデータ内部に、dict型のデータを単純にネストすることはできないようである。何かしらの方法でアクセスできないデータが dict型のデータの内部で発生すると、以下のようなエラーが出るようである。

<<<{"command":"info","UUID":"94035132-f796-4b75-bb16-6619d8bf7bc1","data":null}
connection handler failed
Traceback (most recent call last):
  File "/usr/lib/python3.13/site-packages/websockets/asyncio/server.py", line 376, in conn_handler
    await self.handler(connection)
  File "/home/username/work/OneNoteAlternative/firstPrototype/backend/dataServer/main.py", line 13, in mainLoop
    await receiveLoop(websocket,controller.controler)
  File "/home/username/work/OneNoteAlternative/firstPrototype/backend/dataServer/helper/netwrok.py", line 23, in receiveLoop
    await callback(message,websocket)
  File "/home/username/work/OneNoteAlternative/firstPrototype/backend/dataServer/controller.py", line 41, in controler
    await commands[requestedCommand](request,websocket)
  File "/home/username/work/OneNoteAlternative/firstPrototype/backend/dataServer/commands/info.py", line 88, in info
    "data": {
            ^
        notebookJSONinfo
        ^^^^^^^^^^^^^^^^
    }
    ^
TypeError: unhashable type: 'dict'

notebookJSONinfo の中身

{
    'test':{
        'name': 'test',
        'createDate': '2025/09/13',
        'updateDate': '2025/09/13',
        'id': 'bb6f7d1c-c722-495c-923b-ab9e9574ef5b', 
        'pages': ['test.md', 'test.json'], 
        'files': ['testfile.txt']
    }
}

問題のコード

 以下の状態だと、data key の中に、アクセスできないデータ (notebookJSONinfo) が発生している。

# 何かしらのコード

notebookJSONinfo = findNote

# 何かしらのコード
    await websocket.send(json.dumps({
        "status"        : "ok",
        "UUID"          : request["UUID"],
        "command"       : "info",
        "errorMessage"  : "nothing",
        "data"          : { # 新しい dict 型の変数を宣言してしまっている
            # notebookJSONinfo にアクセスするための key が割り当てられていないため、エラーになる。
            notebookJSONinfo    
        }
    }))
# 何かしらのコード

 notebookJSONinfo の中身を直接書いてみると、以下のようになる。

# 何かしらのコード

notebookJSONinfo = findNote

# 何かしらのコード
    await websocket.send(json.dumps({
        "status"        : "ok",
        "UUID"          : request["UUID"],
        "command"       : "info",
        "errorMessage"  : "nothing",
        "data"          : {  
            { # この dict 型の変数にアクセスするために使える key が存在しない。 
                'test':{
                    'name': 'test',
                    'createDate': '2025/09/13',
                    'updateDate': '2025/09/13',
                    'id': 'bb6f7d1c-c722-495c-923b-ab9e9574ef5b', 
                    'pages': ['test.md', 'test.json'], 
                    'files': ['testfile.txt']
                }
            }
        }
    }))
# 何かしらのコード

問題の解消

 実装を煩雑にしたくないので、単純に notebookJSONinfo を直接 data key の内容として割り当てることで、ドキュメントに書いた通りのレスポンスをしつつ、問題を解消した。

# 何かしらのコード

notebookJSONinfo = findNote

# 何かしらのコード
    await websocket.send(json.dumps({
        "status"        : "ok",
        "UUID"          : request["UUID"],
        "command"       : "info",
        "errorMessage"  : "nothing",
        # 単純に直接 data key に notebookJSONinfo をぶら下げることで、notebookJSONinfo 自体にアクセス不能になることを防いだ
        "data"          : notebookJSONinfo
    }))
# 何かしらのコード
# 何かしらのコード

notebookJSONinfo = findNote

# 何かしらのコード
    await websocket.send(json.dumps({
        "status"        : "ok",
        "UUID"          : request["UUID"],
        "command"       : "info",
        "errorMessage"  : "nothing",
        # 単純に直接 data key に notebookJSONinfo をぶら下げることで、notebookJSONinfo 自体にアクセス不能になることを防いだ
        "data"          : {
                            'test':{
                                'name': 'test',
                                'createDate': '2025/09/13',
                                'updateDate': '2025/09/13',
                                'id': 'bb6f7d1c-c722-495c-923b-ab9e9574ef5b', 
                                'pages': ['test.md', 'test.json'], 
                                'files': ['testfile.txt']
                            }
                        }
    }))
# 何かしらのコード

firstPrototype の README を更新した

 実際にシステムを試す手順を他人にわかるように書いていなかったので、書き足したりした。

How to start

install libraries

cd firstPrototype/backend/dataServer
pip install -r requirements.txt
cd "firstPrototype/frontend/onenote alternative frontend"
npm install

start this system

start the clientside backend
# Currently not implemented. You don't have to start the clientside backend.
start the dataserver backend
cd firstPrototype/backend/dataServer
python main.py
start the frontend
cd "firstPrototype/frontend/onenote alternative frontend"
npm run dev

ページ削除に関して

 ノートブック内のページを削除した場合はすぐには削除せずに、一定期間保持する仕様にしたいことを書いた。

delete pages

When delete pages, those will not be immediately removed from the filesystem. Those are kept for a certain period of time. User can avoid accidentally delete pages.

参考にしたサイトとか