Skip to content

Commit 2be2c9a

Browse files
authored
Merge pull request #1 from robotpy/master
Updating to latest changes
2 parents db9e5c5 + 37756dc commit 2be2c9a

30 files changed

+1457
-283
lines changed

.travis.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
language: python
3+
services:
4+
- docker
5+
6+
install:
7+
- pip install sphinx sphinx-rtd-theme
8+
9+
script:
10+
- make -C docs html
11+
- ./build_exe.sh
12+
13+
jobs:
14+
include:
15+
- stage: format-check
16+
python:
17+
- "3.6"
18+
install:
19+
- pip install black
20+
script:
21+
- black --check --diff .
22+
23+
deploy:
24+
- provider: pypi
25+
user: $PYPI_USERNAME
26+
password: $PYPI_PASSWORD
27+
on:
28+
tags: true
29+
30+
- provider: releases
31+
api_key: $GITHUB_OAUTH_TOKEN
32+
file_glob: true
33+
file: dist/pynetworktables2js*.zip
34+
skip_cleanup: true
35+
on:
36+
tags: true
37+
38+
notifications:
39+
webhooks:
40+
urls:
41+
- https://webhooks.gitter.im/e/2fb1c026e64fdd70d27b
42+
on_success: change
43+
on_failure: always
44+
on_start: never

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
include requirements.txt
12
recursive-include example *
23
recursive-include pynetworktables2js/js *

README.rst

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pynetworktables2js
22
==================
33

4-
A cross platform library that forwards NetworkTables key/values over a Websocket,
4+
A cross platform library that forwards NetworkTables key/values over a WebSocket,
55
so that you can easily write a Driver Station Dashboard for your robot in HTML5 +
66
JavaScript.
77

88
This library does not provide a full dashboard solution, but is intended to
99
provide the necessary plumbing for one to create one with only knowledge
10-
of HTML/Javascript. Because the communications layer uses NetworkTables, you
10+
of HTML/JavaScript. Because the communications layer uses NetworkTables, you
1111
can connect to all FRC languages (C++, Java, LabVIEW, Python).
1212

1313
.. note:: NetworkTables is a protocol used for robot communication in the
@@ -23,6 +23,22 @@ Documentation can be found at http://pynetworktables2js.readthedocs.org/
2323
Installation
2424
============
2525

26+
Easy install (Windows only)
27+
---------------------------
28+
29+
1. Download the latest pynetworktables2js.exe from GitHub at
30+
https://github.com/robotpy/pynetworktables2js/releases .
31+
2. Extract the exe from the zipfile, and copy it to your directory of HTML/JS
32+
files.
33+
3. Double click the exe to run it!
34+
35+
.. note:: By default, it will connect to 127.0.0.1. To connect to a robot,
36+
you will need to pass the exe arguments to tell it where the robot is.
37+
Use ``--help`` to see the available options.
38+
39+
Manual install
40+
--------------
41+
2642
Make sure to install python 2 or 3 on your computer, and on Windows you can
2743
execute::
2844

@@ -56,13 +72,47 @@ those resources to make something cool to control your robot?
5672
Usage
5773
=====
5874

75+
You can just distribute your HTML files, and run a pynetworktables server
76+
using the following command from inside the directory::
77+
78+
python -m pynetworktables2js
79+
80+
Or on Windows::
81+
82+
py -m pynetworktables2js
83+
84+
This will start a pynetworktables2js server using Tornado (which is installed
85+
by default) and it will serve the current directory. You can navigate your
86+
browser (I recommend Chrome) to http://127.0.0.1:8888 and see your website.
87+
88+
You will want to also pass either the ``--robot`` or ``--team`` switch::
89+
90+
py -m pynetworktables2js --robot roborio-XXXX-frc.local
91+
py -m pynetworktables2js --team XXXX
92+
93+
Dashboard mode currently doesn't work, as the underlying support in
94+
pynetworktables hasn't been implemented yet for the newer FRC Driver Station.
95+
96+
Customized python server
97+
------------------------
98+
99+
There are two example servers distributed with pynetworktables2js, one that
100+
uses `tornado <http://www.tornadoweb.org/en/stable/>`_, and one that uses
101+
`aiohttp <https://github.com/KeepSafe/aiohttp>`_. Either one should work.
102+
59103
Go to the 'example' directory distributed with pynetworktables2js, and run::
60104

61-
python server.py --robot 127.0.0.1
105+
python tornado_server.py --robot 127.0.0.1
62106

63107
If you want to try this out with your current robot, you can do::
64108

65-
python server.py --robot roborio-XXX.local
109+
python tornado_server.py --robot roborio-XXX.local
110+
111+
If you are running pynetworktables2js on your driver station laptop, you can
112+
receive robot IP information directly from the Driver Station (handy during
113+
actual competitions)::
114+
115+
python tornado_server.py --dashboard
66116

67117
If you navigate your browser (I recommend Chrome) to http://127.0.0.1:8888, all
68118
of the current NetworkTables values will be shown as they change.
@@ -77,18 +127,19 @@ dashboard. Just add your custom files to the www directory.
77127
Contributing new changes
78128
========================
79129

80-
pynetworktables2js is intended to be a project that all members of the FIRST
130+
pynetworktables2js is intended to be a project that all members of the `FIRST`
81131
community can quickly and easily contribute to. If you find a bug, or have an
82132
idea that you think others can use:
83133

84-
1. `Fork this git repository <https://github.com/robotpy/pynetworktables2js/fork>`_ to your github account
134+
1. `Fork this git repository <https://github.com/robotpy/pynetworktables2js/fork>`_
135+
to your GitHub account
85136
2. Create your feature branch (``git checkout -b my-new-feature``)
86137
3. Commit your changes (``git commit -am 'Add some feature'``)
87138
4. Push to the branch (``git push -u origin my-new-feature``)
88-
5. Create new Pull Request on github
139+
5. Create new Pull Request on GitHub
89140

90141
One place in particular I would love to see contributions is in adding useful
91-
javascript functions/objects that make creating dashboards even easier!
142+
JavaScript functions/objects that make creating dashboards even easier!
92143

93144
Authors
94145
=======

build_exe.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
#
3+
# Builds a pyinstaller-based Windows exe for pynetworktables2js
4+
#
5+
6+
cd $(dirname $0)
7+
docker run --rm -v "$(pwd):/src:Z" -e PYTHONPATH=/src cdrx/pyinstaller-windows:python2
8+
docker run --rm -v "$(pwd):/src:Z" --entrypoint /bin/bash cdrx/pyinstaller-windows:python2 -c "chown -R $(id -u $USERNAME) /src/dist"
9+
10+
zip dist/pynetworktables2js-$(git describe --tags).zip dist/windows/pynetworktables2js.exe

docs/api_camera.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
JS API: Camera Integration
2+
==========================
3+
4+
.. code-block:: html
5+
6+
<script src="/networktables/camera.js"></script>
7+
8+
.. note:: These functions require `jQuery <http://jquery.com/>`_ to be
9+
loaded first!
10+
11+
.. js:function:: loadCameraOnConnect(args)
12+
13+
This useful helper function will create an img or svg element inside of the
14+
div element that you specify. The image will only be connected when a
15+
successful NetworkTables connection is detected, to prevent timeout issues.
16+
Additionally, this function will attempt to verify that the webcam server is
17+
actually up and running before creating the image.
18+
19+
You should provide an object with an object that can have the following
20+
attributes:
21+
22+
:param container: Where to draw things
23+
:param proto: optional, defaults to http://
24+
:param host: optional, if null will use robot's autodetected IP
25+
:param port: optional, webserver port
26+
:param image_url: path to mjpg stream
27+
:param data_url: a file or page that must exist on the webcam server
28+
:param wait_img: optional image to show when not connected
29+
:param error_img: optional image to show when not connected
30+
:param attrs: optional attributes to set on svg or img element
31+
:param nosim: if true, connect to the webcam in simulation mode
32+
33+
For example, to connect to mjpg-streamer on the RoboRIO:
34+
35+
.. code-block:: javascript
36+
37+
loadCameraOnConnect({
38+
container: '#my_div_element',
39+
port: 5800,
40+
image_url: '/?action=stream',
41+
data_url: '/program.json',
42+
attrs: {
43+
width: 640,
44+
height: 480
45+
}
46+
});
47+
48+
49+
.. note:: This has only been tested with mjpg-streamer, but should work for
50+
other HTTP webcams as well.

docs/api_jquery_ext.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
JS API: JQuery Extensions
2+
=========================
3+
4+
.. code-block:: html
5+
6+
<script src="/networktables/jquery_ext.js"></script>
7+
8+
.. note:: These functions require `jQuery <http://jquery.com/>`_ to be
9+
loaded first!
10+
11+
.. js:function:: $.nt_toggle(key, function)
12+
13+
When a networktables variable changes, a checkbox element will be updated
14+
when a NT variable changes and when a user clicks it.
15+
16+
Alternatively, you can use this with custom elements, by providing a function
17+
that will be called only when the NT value is changed. The NT value will be
18+
toggled when the user clicks the selected element(s).
19+
20+
:param k: Networktables key
21+
:param fn: (optional) function that accepts a single param, will be called on change
22+
:param evt: (optional) Which event to toggle the value on (defaults to 'click')
23+
24+
Example usage:
25+
26+
.. code-block:: javascript
27+
28+
// this works on a checkbox
29+
$('#my_checkbox').nt_toggle('/SmartDashboard/some_boolean');
30+
31+
// or on a clickable element
32+
$('#my_clickable').nt_toggle('/SmartDashboard/b', function(v) {
33+
this.css('background-color', v ? 'green' : 'gray');
34+
});

docs/api_js.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
JavaScript API
2-
==============
1+
JS API: NetworkTables
2+
=====================
3+
4+
To use these functions, add this to your HTML page:
5+
6+
.. code-block:: html
7+
8+
<script src="/networktables/networktables.js"></script>
9+
10+
**Note**:
311

412
It's very important to note that the Javascript NetworkTables API currently
513
has no concept of a table or subtable. When referring to keys when accessing
@@ -35,6 +43,9 @@ These functions allow your code to listen for particular NetworkTables events.
3543
Sets a function to be called when the robot connects/disconnects to the
3644
pynetworktables2js server via NetworkTables. It will also be called when
3745
the websocket connects/disconnects.
46+
47+
When a listener function is called with a 'true' parameter, the
48+
NetworkTables.getRobotAddress() function will return a non-null value.
3849

3950
:param f: a function that will be called with a single boolean parameter
4051
that indicates whether the robot is connected
@@ -93,11 +104,17 @@ NetworkTables Interface
93104

94105
:param key: A networktables key
95106
:returns: true if a key is present in NetworkTables, false otherwise
107+
108+
.. warning:: This may not return correct results when the websocket is not
109+
connected
96110

97111
.. js:function:: NetworkTables.getKeys()
98112

99113
:returns: all the keys in the NetworkTables
100114

115+
.. warning:: This may not return correct results when the websocket is not
116+
connected
117+
101118
.. js:function:: NetworkTables.getValue(key[, defaultValue])
102119

103120
Returns the value that the key maps to. If the websocket is not
@@ -107,10 +124,17 @@ NetworkTables Interface
107124
:param defaultValue: If the key isn't present in the table, return this instead
108125
:returns: value of key if present, ``undefined`` or ``defaultValue`` otherwise
109126

127+
.. warning:: This may not return correct results when the websocket is not
128+
connected
129+
110130
.. note:: To make a fully dynamic webpage that updates when the robot
111131
updates values, it is recommended (and simpler) to use
112132
:func:`addKeyListener` or :func:`addGlobalListener` to listen
113133
for changes to values, instead of using this function.
134+
135+
.. js:function:: NetworkTables.getRobotAddress()
136+
137+
:returns: null if the robot is not connected, or a string otherwise
114138

115139
.. js:function:: NetworkTables.isRobotConnected()
116140

docs/api_util.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
JS API: Utilities
2+
=================
3+
4+
To use these functions, add this to your HTML page:
5+
6+
.. code-block:: html
7+
8+
<script src="/networktables/utils.js"></script>
9+
10+
.. note:: These functions require `jQuery <http://jquery.com/>`_ and
11+
`D3 <https://d3js.org/>`_ to be loaded first!
12+
13+
The functions in this file are still experimental in nature, and as we
14+
expand the number of functions in this file it is expected that the API
15+
will change.
16+
17+
SendableChooser
18+
---------------
19+
20+
.. js:function:: attachSelectToSendableChooser(html_id, nt_key)
21+
22+
Given the id of an HTML ``<select>`` element and the key name of a SendableChooser
23+
object setup in networktables, this will sync the select combo box with the
24+
contents of the SendableChooser, and you will be able to select an object
25+
using the select element.
26+
27+
:param html_id: An ID of an HTML select element
28+
:param nt_key: The name of the NetworkTables key that the SendableChooser
29+
is associated with
30+
31+
See the WPILib documentation for information on how to use SendableChooser
32+
in your robot's program.
33+
34+
.. js:function:: updateSelectWithChooser(html_id, nt_key)
35+
36+
This function is designed to be used from the onValueChanged callback
37+
whenever values from a SendableChooser change, but you probably should
38+
prefer to use attachSelectToSendableChooser instead.
39+
40+
See attachSelectToSendableChooser documentation.
41+
42+
Indicators
43+
----------
44+
45+
.. js:function:: attachRobotConnectionIndicator(html_id[, size, stroke_width])
46+
47+
Creates a circle SVG that turns red when robot is not connected, green when
48+
it is connected.
49+
50+
:param html_id: ID to insert svg into
51+
:param size: Size of circle
52+
:param stroke_width: Border of circle

0 commit comments

Comments
 (0)