Skip to content

Commit a72dbaa

Browse files
committed
set demos as console scripts, update chat instructions
1 parent bc90632 commit a72dbaa

File tree

9 files changed

+77
-38
lines changed

9 files changed

+77
-38
lines changed

docs/examples.chat.rst

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
Chat Demo
22
=========
33

4-
Copy the code below into a file called ``chat.py``.
5-
Install dependencies, preferably in a virtual environment, with:
4+
This example demonstrates how to create a simple chat application using libp2p.
65

7-
.. code-block:: bash
6+
.. code-block:: console
87
9-
python -m pip install libp2p
8+
$ python -m pip install libp2p
9+
Collecting libp2p
10+
...
11+
Successfully installed libp2p-x.x.x
12+
$ chat-demo
13+
Run this from the same folder in another console:
1014
15+
chat-demo -p 8001 -d /ip4/127.0.0.1/tcp/8000/p2p/QmPouApKqyxJDy6YT21EXNS6efuNzvJ3W3kqRQxkQ77GFJ
1116
12-
Run the demo with ``python chat.py`` and copy the output.
17+
Waiting for incoming connection...
1318
14-
Open a second terminal, navigate to the folder that contains ``chat.py``, then paste
15-
and run the copied line.
19+
Copy the line that starts with ``chat-demo -p 8001``, open a new terminal in the same
20+
folder and paste it in:
21+
22+
.. code-block:: console
23+
24+
$ chat-demo -p 8001 -d /ip4/127.0.0.1/tcp/8000/p2p/QmPouApKqyxJDy6YT21EXNS6efuNzvJ3W3kqRQxkQ77GFJ
25+
Connected to peer /ip4/127.0.0.1/tcp/8000
26+
27+
You can then start typing messages in either terminal and see them relayed to the
28+
other terminal. To exit the demo, send a keyboard interrupt (``Ctrl+C``) in either terminal.
29+
30+
The full source code for this example is below:
1631

1732
.. literalinclude:: ../examples/chat/chat.py
1833
:language: python

docs/examples.echo.rst

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
Echo Demo
22
=========
33

4-
Copy the code below into a file called ``demo.py``.
5-
Install dependencies, preferably in a virtual environment, with:
4+
This example demonstrates a simple ``echo`` protocol.
65

7-
.. code-block:: bash
6+
.. code-block:: console
87
9-
python -m pip install libp2p
8+
$ python -m pip install libp2p
9+
Collecting libp2p
10+
...
11+
Successfully installed libp2p-x.x.x
12+
$ echo-demo
13+
Run this from the same folder in another console:
1014
11-
Run the demo with ``python echo.py`` and copy the output.
15+
echo-demo -p 8001 -d /ip4/127.0.0.1/tcp/8000/p2p/16Uiu2HAmAsbxRR1HiGJRNVPQLNMeNsBCsXT3rDjoYBQzgzNpM5mJ
1216
13-
Open a second terminal, navigate to the folder that contains ``echo.py``, then paste
14-
and run the copied line.
17+
Waiting for incoming connection...
18+
19+
Copy the line that starts with ``echo-demo -p 8001``, open a new terminal in the same
20+
folder and paste it in:
21+
22+
.. code-block:: console
23+
24+
$ echo-demo -p 8001 -d /ip4/127.0.0.1/tcp/8000/p2p/16Uiu2HAmAsbxRR1HiGJRNVPQLNMeNsBCsXT3rDjoYBQzgzNpM5mJ
25+
26+
I am 16Uiu2HAmE3N7KauPTmHddYPsbMcBp2C6XAmprELX3YcFEN9iXiBu
27+
Sent: hi, there!
28+
29+
Got: hi, there!
1530
1631
.. literalinclude:: ../examples/echo/echo.py
1732
:language: python

docs/examples.ping.rst

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
Ping Demo
22
=========
33

4-
Copy the code below into a file called ``ping.py``.
5-
Install dependencies, preferably in a virtual environment, with:
4+
This example demonstrates how to use the libp2p ``ping`` protocol.
65

7-
.. code-block:: bash
6+
.. code-block:: console
87
9-
python -m pip install libp2p
8+
$ python -m pip install libp2p
9+
Collecting libp2p
10+
...
11+
Successfully installed libp2p-x.x.x
12+
$ ping-demo
13+
Run this from the same folder in another console:
1014
11-
Run the demo with ``python ping.py`` and copy the output.
15+
ping-demo -p 8001 -d /ip4/127.0.0.1/tcp/8000/p2p/QmXfptdHU6hqG95JswxYVUH4bphcK8y18mhFcgUQFe6fCN
1216
13-
Open a second terminal, navigate to the folder that contains ``ping.py``, then paste
14-
and run the copied line.
17+
Waiting for incoming connection...
18+
19+
Copy the line that starts with ``ping-demo -p 8001``, open a new terminal in the same
20+
folder and paste it in:
21+
22+
.. code-block:: console
23+
24+
$ ping-demo -p 8001 -d /ip4/127.0.0.1/tcp/8000/p2p/QmXfptdHU6hqG95JswxYVUH4bphcK8y18mhFcgUQFe6fCN
25+
sending ping to QmXfptdHU6hqG95JswxYVUH4bphcK8y18mhFcgUQFe6fCN
26+
received pong from QmXfptdHU6hqG95JswxYVUH4bphcK8y18mhFcgUQFe6fCN
27+
28+
The full source code for this example is below:
1529

1630
.. literalinclude:: ../examples/echo/echo.py
1731
:language: python

docs/examples.rst

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
Examples
22
========
33

4-
These are functional demonstrations of aspects of the library. They are
5-
intended to be run as scripts, and are not intended to be used as part of
6-
another application.
7-
8-
Example Scripts
9-
---------------
4+
These are functional demonstrations of aspects of the library.
105

116
.. toctree::
127

138
examples.chat
149
examples.echo
1510
examples.ping
16-
17-
Module contents
18-
---------------
19-
20-
.. automodule:: examples
21-
:members:
22-
:undoc-members:
23-
:show-inheritance:

examples/chat/chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async def stream_handler(stream: INetStream) -> None:
5454

5555
print(
5656
"Run this from the same folder in another console:\n\n"
57-
f"python chat.py -p {int(port) + 1} "
57+
f"chat-demo -p {int(port) + 1} "
5858
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
5959
)
6060
print("Waiting for incoming connection...")

examples/echo/echo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async def run(port: int, destination: str, seed: int = None) -> None:
5353

5454
print(
5555
"Run this from the same folder in another console:\n\n"
56-
f"python echo.py -p {int(port) + 1} "
56+
f"echo-demo -p {int(port) + 1} "
5757
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
5858
)
5959
print("Waiting for incoming connections...")

examples/ping/ping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async def run(port: int, destination: str) -> None:
6565

6666
print(
6767
"Run this from the same folder in another console:\n\n"
68-
f"python ping.py -p {int(port) + 1} "
68+
f"ping-demo -p {int(port) + 1} "
6969
f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}\n"
7070
)
7171
print("Waiting for incoming connection...")

newsfragments/490.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Create entry points for demos to be run directly from installed package

setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,11 @@
111111
"Programming Language :: Python :: 3.12",
112112
],
113113
platforms=["unix", "linux", "osx"],
114+
entry_points={
115+
"console_scripts": [
116+
"chat-demo=examples.chat.chat:main",
117+
"echo-demo=examples.echo.echo:main",
118+
"ping-demo=examples.ping.ping:main",
119+
],
120+
},
114121
)

0 commit comments

Comments
 (0)