Skip to content

Fix cirq_web README from large Typescript development PR merge #4314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 169 commits into from
Jul 15, 2021
Merged
Changes from all commits
Commits
Show all changes
169 commits
Select commit Hold shift + click to select a range
4648e65
add node_modules to .gitignore, add three.js to package-lock
seunomonije May 11, 2021
621b729
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije May 11, 2021
8832853
initial refactoring
seunomonije May 12, 2021
5516b55
move package-lock into the web subdirectory
seunomonije May 12, 2021
7a2aacd
readme, slight refactor
seunomonije May 14, 2021
bbf8af2
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije May 14, 2021
490a8b2
add live-server
seunomonije May 14, 2021
a08a196
added more to readme file
seunomonije May 14, 2021
b3a3a7e
prettier
seunomonije May 14, 2021
2c66c8a
small changes to readme
seunomonije May 14, 2021
bb015e0
typescript
seunomonije May 14, 2021
eb1a435
small change to readme
seunomonije May 14, 2021
724a9da
hacking endpoint from friday
seunomonije May 17, 2021
86199a6
refactor/progress dump
seunomonije May 19, 2021
1f52ac8
post gts lint. The extra interface files might not stay.
seunomonije May 19, 2021
391258d
rename to cirq-web
seunomonije May 21, 2021
2650381
refactor and additional files
seunomonije May 21, 2021
064de3f
refactor again
seunomonije May 21, 2021
be5564c
updated setup
seunomonije May 21, 2021
6d8b533
init file
seunomonije May 21, 2021
eddb58f
refactor, split cirq-web into its own package
seunomonije May 21, 2021
8015fac
update versions to match
seunomonije May 21, 2021
1557b52
add cirq-web autopkg to script
seunomonije May 21, 2021
ee9f890
moved python init
seunomonije May 21, 2021
09127f8
small test changes cuz colab is being difficult
seunomonije May 21, 2021
df2e359
new bundle to upload
seunomonije May 21, 2021
03e8bd0
lowercase cirq
seunomonije May 21, 2021
f574154
fix dev browser environment
seunomonije May 21, 2021
c24ffe3
pre presentation
seunomonije May 26, 2021
e78d85d
post presentation state
seunomonije May 26, 2021
21343fb
very very very rough bloch sphere
seunomonije May 27, 2021
37f1218
rough vector support end to end
seunomonije May 27, 2021
6351f77
updated with working HTML hot reloading and example bloch_sphere in j…
seunomonije May 27, 2021
29ec82f
progress dump
seunomonije Jun 3, 2021
cf9b435
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jun 3, 2021
807b4ff
refactored Bloch Sphere organization
seunomonije Jun 3, 2021
731d194
new directory structure, standalone HTML files, Python integration, n…
seunomonije Jun 4, 2021
5ab03c5
small changes for webpack continuity and organization
seunomonije Jun 4, 2021
51ea689
added comments to ts components
seunomonije Jun 4, 2021
c7cf8c9
rename cirq_python to cirq_web
seunomonije Jun 4, 2021
ab8febe
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jun 4, 2021
b33eb1e
added first test
seunomonije Jun 4, 2021
3fedccd
added preliminary testing to cirq_web
seunomonije Jun 4, 2021
b4ecff1
recommit
seunomonije Jun 4, 2021
12bc26b
testing covg, updated readmes, documentation, other
seunomonije Jun 7, 2021
e6723da
todo readme
seunomonije Jun 8, 2021
416b126
move gitignore
seunomonije Jun 8, 2021
c07c561
fix gitignore
seunomonije Jun 8, 2021
57df8aa
cleanup
seunomonije Jun 8, 2021
fce0580
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jun 8, 2021
f2eec63
remove bloch sphere
seunomonije Jun 8, 2021
918f77e
remove files.js
seunomonije Jun 8, 2021
bd5d1b4
add dist for path issues, may need to refactor to keep out later
seunomonije Jun 8, 2021
39ab966
adding support for cirq-web dependencies in the pytest-minimal test
seunomonije Jun 8, 2021
44d62b6
quick fix to pytest-minimal
seunomonije Jun 8, 2021
10bd505
explicit utf-8 encoding for windows systems
seunomonije Jun 8, 2021
08e48a5
quick addition
seunomonije Jun 8, 2021
8300306
add final explicit encoding
seunomonije Jun 8, 2021
83297ef
deciding to use mocha for testing
seunomonije Jun 8, 2021
802f9d2
testing framework setup with simple test run
seunomonije Jun 8, 2021
c431503
Update cirq-web/cirq_web/bloch_sphere/cirq_bloch_sphere.py
seunomonije Jun 8, 2021
0107a58
Update cirq-web/cirq_ts/_version.py
seunomonije Jun 8, 2021
bbc844e
Update cirq-web/cirq_ts/src/bloch_sphere/components/Axes.class.d.ts
seunomonije Jun 8, 2021
0ccdb90
Update cirq-web/cirq_ts/src/bloch_sphere/components/Axes.class.ts
seunomonije Jun 8, 2021
6f5597f
Update cirq-web/cirq_ts/src/bloch_sphere/components/Meridians.class.ts
seunomonije Jun 8, 2021
cd1c7a0
Update cirq-web/cirq_web/bloch_sphere/cirq_bloch_sphere.py
seunomonije Jun 8, 2021
b3d297d
Update cirq-web/setup.py
seunomonije Jun 8, 2021
af02537
Update cirq-web/cirq_ts/src/bloch_sphere/components/Meridians.class.ts
seunomonije Jun 8, 2021
b2045e9
Update cirq-web/cirq_ts/src/bloch_sphere/components/Meridians.class.ts
seunomonije Jun 8, 2021
efcdac1
Update cirq-web/cirq_ts/src/bloch_sphere/components/Meridians.class.ts
seunomonije Jun 8, 2021
d489643
Update cirq-web/cirq_ts/src/bloch_sphere/components/Vector.class.ts
seunomonije Jun 8, 2021
a758965
small changes
seunomonije Jun 8, 2021
2f0c502
Merge branch 'master' of https://github.com/seunomonije/Cirq
seunomonije Jun 8, 2021
a343878
small fixes commit
seunomonije Jun 8, 2021
bc4d3de
first round of comment resolutions, added tiny visualization testing …
seunomonije Jun 9, 2021
227b56f
cirq_ts readme PR comment fixes
seunomonije Jun 9, 2021
0a46d3f
PR comment fixes BlochSphere.ts
seunomonije Jun 9, 2021
bc97a60
PR comment fixes meridians
seunomonije Jun 9, 2021
576031b
PR comment fixes scene
seunomonije Jun 9, 2021
54b2e1b
PR comment fixes vector and test
seunomonije Jun 9, 2021
28467fb
double check fixes from PR comments
seunomonije Jun 9, 2021
0a5f6bc
rename files, work on text.ts
seunomonije Jun 9, 2021
845a944
temp change of bs symbols, quick/dirty but working e2e image check te…
seunomonije Jun 10, 2021
2c80848
ran the linter, fixed .eslintrc.json so that it reports no errors
seunomonije Jun 10, 2021
7657584
add preliminary CI
seunomonije Jun 10, 2021
70887eb
CI for unit testing
seunomonije Jun 10, 2021
b80007b
fix rename for ts-test
seunomonije Jun 10, 2021
8ad3ef4
rename library to CirqTS
seunomonije Jun 10, 2021
2527c57
can configure div id of the bloch sphere
seunomonije Jun 10, 2021
7bce91e
add vector type
seunomonije Jun 10, 2021
250ea94
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jun 10, 2021
3a6acd6
update bundle and jupyter notebook
seunomonije Jun 10, 2021
6aabc66
small refactor of python, added more to README
seunomonije Jun 10, 2021
71ff76d
pylint
seunomonije Jun 10, 2021
a803e11
removed IPython as a requirement
seunomonije Jun 11, 2021
3c3bafc
added TS testing frameworks, rough TS viz and unit testing, small rea…
seunomonije Jun 14, 2021
f6e48bb
restart ci
seunomonije Jun 14, 2021
a49fcbc
test ci
seunomonije Jun 14, 2021
3451a3a
add back coverage testing
seunomonije Jun 14, 2021
20b314c
fixed merge conflicts
seunomonije Jun 14, 2021
d3fdd5a
make check/ts-coverage an executable
seunomonije Jun 14, 2021
4bb2242
rough text changes, fix ipython fails on CI
seunomonije Jun 14, 2021
b512c9f
move _version.py to the correct spot
seunomonije Jun 14, 2021
8769ffc
quick updates
seunomonije Jun 14, 2021
b8d5926
fix space in file
seunomonije Jun 15, 2021
e7ba83f
updated ts testing framework, fixed labels
seunomonije Jun 15, 2021
cbb6930
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jun 15, 2021
278c911
linting and merge w upstream
seunomonije Jun 15, 2021
3b5a513
add copyrights, small PR comment changes
seunomonije Jun 15, 2021
bdc7955
fix naming of divId
seunomonije Jun 15, 2021
16ec03a
ts e2e testing writes to temp file
seunomonije Jun 15, 2021
9bdbc47
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jun 15, 2021
3e1054d
exclude utility functions from testing
seunomonije Jun 15, 2021
599a8bd
updated testing portion of ts README
seunomonije Jun 15, 2021
8820d39
updated README, example notebook, fixed bloch sphere vector point, sm…
seunomonije Jun 16, 2021
b41057c
linting and cleanup changes
seunomonije Jun 16, 2021
4d6bffc
deleting bloch sphere due to ci issues
seunomonije Jun 16, 2021
2914c0d
redo expected png
seunomonije Jun 16, 2021
b259678
add utf-8 encoding to test
seunomonije Jun 16, 2021
4b0da2d
second attempt to fix image diff issue
seunomonije Jun 16, 2021
e48596f
fix test to account for unicode diff threshold
seunomonije Jun 16, 2021
2fe2377
update cirq-web readme
seunomonije Jun 16, 2021
625659e
add utf-8 charset for readme
seunomonije Jun 16, 2021
d2e37ea
add cirq_ts to setup.py
seunomonije Jun 16, 2021
9f403bf
change setup.py for new packaging structure
seunomonije Jun 16, 2021
e314d19
add ts distribution files to the python package
seunomonije Jun 16, 2021
045fbc3
more clarity to package searching
seunomonije Jun 16, 2021
1976d8c
add .js extension
seunomonije Jun 16, 2021
3a6e0a6
add manifest file to help get the bundles
seunomonije Jun 16, 2021
cc23aee
change to setup.py
seunomonije Jun 16, 2021
6a619dd
reset setup.py
seunomonije Jun 16, 2021
44a9521
include package data tag for manifest to work
seunomonije Jun 16, 2021
eb0d7a4
testing removal of cirq_ts from packagin, we don't need it to have an…
seunomonije Jun 16, 2021
bae0b7c
adding cirq_ts back, we need it
seunomonije Jun 16, 2021
ede563f
formatting fix
seunomonije Jun 16, 2021
12fdbc7
remove packaging from readme
seunomonije Jun 17, 2021
22ed261
Merge branch 'master' into master
seunomonije Jun 17, 2021
c187c49
typescript PR fixes
seunomonije Jun 17, 2021
6e2e549
pr fixes for python
seunomonije Jun 17, 2021
969d236
change fixes
seunomonije Jun 17, 2021
618ff01
missed fixes
seunomonije Jun 17, 2021
7f9536e
progress on new testing
seunomonije Jun 21, 2021
5469659
another refactor of the testing framework
seunomonije Jun 21, 2021
7ff32f7
update meridians test
seunomonije Jun 21, 2021
27ed7cb
major typescript code and testing refactor, implemented PR comments
seunomonije Jun 22, 2021
b30dd9b
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jun 22, 2021
d59d56a
lint fixes
seunomonije Jun 22, 2021
8998701
PR comment resolutions, additional misc cleanup and fixes
seunomonije Jun 24, 2021
da0c436
Merge branch 'master' into master
seunomonije Jun 24, 2021
e041212
add IPython back for CI
seunomonije Jun 24, 2021
4deb927
pr comment fixes and bug fixes
seunomonije Jun 25, 2021
95e6fa1
added uuids to bloch spheres, cleaned up example notebook
seunomonije Jun 25, 2021
72ee0e8
update testing statements, general pr fixes
seunomonije Jun 28, 2021
d32510c
fix axes
seunomonije Jun 28, 2021
16ae383
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jul 1, 2021
553dde8
removed json middleman, added more check scripts, general cleanup
seunomonije Jul 1, 2021
59511c6
removed MANIFEST.in and included bundle in package_data
seunomonije Jul 1, 2021
60cc765
changes to pass CI, delete determine_env from widget (YAGNI)
seunomonije Jul 1, 2021
9eb7f29
fix changes after quick scroll through
seunomonije Jul 1, 2021
07c6c50
PR comment resolutions, adding CI for end to end tests, other refacto…
seunomonije Jul 1, 2021
1ab1b76
pr resolutions
seunomonije Jul 2, 2021
f56bdd3
pr changes
seunomonije Jul 2, 2021
28187c7
revert .gitignore
seunomonije Jul 2, 2021
44bb800
rename MockWidget to FakeWidget and format widget.py
seunomonije Jul 2, 2021
9026f65
Merge branch 'master' into master
balopat Jul 2, 2021
675b725
fix merge conflicts
seunomonije Jul 12, 2021
f290bc6
updated cirq-web README
seunomonije Jul 12, 2021
4568181
Merge branch 'master' of https://github.com/quantumlib/Cirq
seunomonije Jul 15, 2021
818d6c2
resolve PR comments
seunomonije Jul 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 36 additions & 37 deletions cirq-web/cirq_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The `cirq_web` package runs separately from the rest of Cirq, and can be used on
A reference for the build structure of a module is the Bloch sphere. Reference the `bloch_sphere/` directory to see the code. Modules should:
- Abide by Cirq convention in terms of testing, styling, and initialization files.
- Contain a "root" folder labeled according to the title of the module. In the case of the Bloch sphere, this is `bloch_sphere/`.
- Contain a main class that contains the code for the visualization. All supporting files should be imported into this class. In the case of the Bloch sphere, this is `cirq_bloch_sphere.py`.
- Contain a main class that contains the code for the visualization. All supporting files should be imported into this class. In the case of the Bloch sphere, this is `bloch_sphere.py`.
- Make sure that any additional modules and files are in separate subdirectories labeled accordingly.

### Developing Python modules for visualization
Expand All @@ -21,60 +21,59 @@ The main class for all visualizations should inherit from the `Widget` class loc
class MyWidget(widget.Widget):
def __init__(self,...):
...
super().__init__('cirq_ts/dist/YOUR_BUNDLE_FILE.js')
super().__init__()
...
```
This will allow you to easily access the absolute path to your bundle file, which should always be used when referencing it, with `self.bundle_file_path`. It also allows you to call `super().get_bundle_script()` to easily
get full script content of your widget as a string. These methods should be used when generating your HTML outputs for boththe web browser and notebooks.
This ensures that your widget has the standard functionality of all Cirq visualization widgets, including:
- A unique id for each instance of your visualization.
- Magic method so that your visualization can be displayed in a Colab/Jupyter notebook.
- The ability to generate a standalone HTML file with your visualization.

The `widget.py` file also contains methods like `write_output_file()` that should be used for all visualizations, as well as some other helpful methods.
`Widget` is an abstract class with methods `get_client_code()` and `get_widget_bundle_name()` that need to be implemented in your visualization as well. Failure to implement these will lead to a `NotImplementedError` at runtime. Instructions on how to properly implement these methods are in the next section.

### Handling HTML output from Python
#### Viewing a visualization in a notebook setting
We capitalize on IPython's `_repr_html_` magic method to help display visualizations in the notebook. In the main class of your visualization, include a method like so:

In your individual visualizations class, you only need to handle two things:
1. The client code that's unique to your visualization.
2. The name of the bundle file.

```python
from cirq_web import widget

class MyWidget(widget.Widget):
...
def _repr_html_(self):
bundle_script = super().get_bundle_script()
def get_client_code(self) -> str:
return f"""
<meta charset="UTF-8">
<div id="container"></div>
{bundle_script}
<script>YOUR_BUNDLE_FUNCTION<script>
<script>
YOUR_CLIENT_CODE
</script>
"""
```
This will allow your visualization to be displayed in a notebook cell with:

def get_widget_bundle_name(self) -> str:
return 'YOUR_BUNDLE_FILE.bundle.js'
```

`Widget` will take this information and organize it so that it can be properly displayed.

#### Viewing a visualization in a notebook setting
We capitalize on IPython's `_repr_html_` magic method to help display visualizations in the notebook. This will allow your visualization to be displayed in a notebook cell with:
```python
widget = MyWidget()
display(widget)
```

#### Generating a standalone HTML file from a visualization
Generating a standalone HTML file is a bit more involved than in a notebook, but not difficult. In the main class of your visualiztion, include a method like so:
You can generate a standalone HTML file of your visualization like so:
```python
import webbrowser
from cirq_web import widget
class MyWidget(widget.Widget):
...
def generate_html_file(self, output_directory='./', file_name="YOUR_VIZ.html", open_in_browser=False):
bundle_script = super().get_bundle_script()
contents = f"""
<meta charset="UTF-8">
<div id="container"></div>
{bundle_script}
<script>YOUR_BUNDLE_FUNCTION<script>
"""
path_of_html_file = widget.write_output_file(output_directory, file_name, contents)

if open_in_browser:
webbrowser.open(str(path_of_html_file), new=2)

return path_of_html_file
```
This code above writes a file named `YOUR_VIZ.html` to the specified output directory, returning the path of the file as a `Path` object. To get the path as string, use `str()`. If the `open_in_browser` flag is used, Python will automatically open your visualization in a new tab using the default browser on your computer.
widget = MyWidget()

The important thing about generating standalone HTML files is that they can be sent and viewed anywhere, regardless of if the recipient has Cirq installed on their computer or not.
output_directory = './'
file_name = 'YOUR_VIZ.html'
open_in_browser = False

widget.generate_html_file(output_directory, file_name, open_in_browser)
```

This code above writes a file named `YOUR_VIZ.html` to the specified output directory, returning the path of the file as a string. If the `open_in_browser` flag is used, Python will automatically open your visualization in a new tab using the default browser on your computer.

The important thing about generating standalone HTML files is that they can be sent and viewed anywhere, regardless of whether the recipient has Cirq installed on their computer or not.