4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NiceGUIでページの状態がユーザ間で共有される問題の対処

Posted at

環境

  • Python 3.10
  • NiceGUI 1.3.11

NiceGUIの概要

NiceGUIを使うと、簡単なPythonコードでWebのGUIを提供できます。類似のツールであるStreamlitに影響を受けていると公式ドキュメントに書いてありますが、Streamlitほど抽象度が高くなくWeb開発の要素が求められる反面、柔軟なアプリケーションを構築しやすい印象です。

現象

NiceGUIで「そのまま」作ったページの状態は、デフォルトではすべてのユーザで共有されます。
文字で説明すると分かりづらいですが、テキスト入力フォームを作って適当な文字列を入力したあと、ブラウザのウィンドウをふたつ開いてみると、状態が共有されてしまっていることがわかると思います。

from nicegui import ui

ui.input()

ui.run()

image.png

これを避けるにはpageデコレータを使います。

from nicegui import ui

@ui.page('/')
def main():
    ui.input()

ui.run()

こうすることで、ページの状態が共有されずユーザごとにプライベートになります。こちらのほうがフォームとして一般的な動作だと思います。

これは公式ドキュメントのPagesのところに書いてあります。

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?