webview python program version 1.0

15 views
Skip to first unread message

Roger Kaufman

unread,
May 10, 2023, 1:42:55 PM5/10/23
to anti...@googlegroups.com
Hi All,

This is what I've been working on for the last few weeks. It is a python
program to open an off file using the web base simple-off-viewer. Since
it is restricted for executables I shared it via a google drive.

https://drive.google.com/file/d/1BbtyDd8G62yRnb_DdV_o9PJTzjfsr4vA/view?usp=share_link

There is a README.txt in the archive including much of what is here.

The major points are...

There is a working director working_dir in webview.py that needs to be
changed to your working directory. Preferably an isolated lowest level
directory for the server to access.

In webview.py there is a url variable currently set to my installation
which will work. There are some browser paths for specific browsers or
you may need to add one. I have it set to Microsoft Edge because I don't
use it for anything and it has few active tabs so its faster.

webview_server is a CORS based temporary server that runs for a few
seconds while the off file is uploaded to the web site. No changes are
needed.

When webview.py runs, webbrowser.get().open() will open a new tab on the
browser. But it returns immediately. There is a 3 second sleep that
works most times before it kills the server. This works most times, but
in my Ubuntu installation I have to have the browser open already for
the page open in time. Being able to know when the page is complete is
the one problem that remained unsolved.

webview.py can take an off file or standard input, so it can be used to
receive a pipe.

Examples are

displays file.off with 50 percent transparency
webview.py file.off -t 0.5

a geodesic rotating
off_util geo_t0_3 | webview.py -rot 1

a geodesic with edges and vertices
off_util geo_t8_8 | webview.py -e 0.04 -v 0.05 -x f

a geodesic with faces hidden
off_util geo_t8_8 | webview.py -x f

a geodesic with edges and vertices hidden
off_color_radial geo_t8_8 -m rainbow | webview.py -x v e

snub dodecahedron with black edges rotating on axis 1,1,0
off_util u29 | webview.py -l -e 0.01 -v 0.01 -rot 0.5 -rotax 1,1,0

open iso_delta f 3 with a black background
iso_delta -c f -s 3 | webview.py -B 000000

A few convenience files were included to be able to open off files to
the browser from file managers. See README.txt

The help for webview.py for version 1.0 is display below.

Enjoy! And if you try it, any comments will be welcome!

Roger

usage: webview.py [-h] [-v VERTEXRADIUS] [-e EDGERADIUS]
                  [-x {v,e,f} [{v,e,f} ...]] [-l] [-t TRANSPARENCY]
                  [-rot ROTATIONSPEED] [-ax ROTATIONAXIS] [-B
BACKGROUNDCOLOR]
                  [--version]
                  [off_file]

Send OFF file to html browser online OFF file viewer.

positional arguments:
  off_file              OFF file. can also be from standard input

optional arguments:
  -h, --help            show this help message and exit
  -v VERTEXRADIUS, --vertexRadius VERTEXRADIUS
                        radius of vertex spheres (default: 0.03)
  -e EDGERADIUS, --edgeRadius EDGERADIUS
                        radius of edge cylinders (default: 0.02)
  -x {v,e,f} [{v,e,f} ...], --hideElements {v,e,f} [{v,e,f} ...]
                        hide elements. to hide vertices, edges and faces
  -l, --blackEdges      paint edges black (default: use defined colors)
  -t TRANSPARENCY, --transparency TRANSPARENCY
                        transparency (default: 0)
  -rot ROTATIONSPEED, --rotationSpeed ROTATIONSPEED
                        rotational speed (default: 0)
  -ax ROTATIONAXIS, --rotationAxis ROTATIONAXIS
                        rotational axis as x,y,z (default: 0,1,0)
  -B BACKGROUNDCOLOR, --backgroundColor BACKGROUNDCOLOR
                        background color in hexadecimal (default: cccccc)
  --version             show program's version number and exit

Wagyx Xygaw

unread,
May 13, 2023, 12:57:55 PM5/13/23
to antiprism
Hi Roger,

Great work putting these scripts together.

I have tested this using the antiviewer shell after activating my conda python.
I had to change a few things on top of the default path for the browser.

I think you could deduce the absolute path of the file using
working_dir = os.path.dirname(os.path.abspath(__file__))

I had to had added python.exe in the command line to run the server
proc = subprocess.Popen([r"python.exe", 'webview_server.py', str(port)])

I have modified the line that deletes the temporary file.
# os.system("rm %s/webview.off" % (working_dir))
os.remove(f"{working_dir}/webview.off")

Finally, here is the command line in the anaconda prompt
python webview.py path_to_of_file.py

Roger Kaufman

unread,
May 13, 2023, 7:09:43 PM5/13/23
to anti...@googlegroups.com
Hi Wagyx,

My motive for using a fixed directory was because the the web server might allow someone to walk down the directory tree. If for some reason the server keeps running. I myself chose d:/webview because it has no sub-directories and the file within it is a copy.

So then of course the copy is deleted which would cause all sorts of trouble is you use the original file in the original path like you are.

Do you think opening the server in any directory is safe? If there is no security issue I could change it to use the origin directory.

I've never heard of Anaconda! It might be why your Popen needs the python.exe. In the prompt it doesn't recognize that webview.py is a script? Are you able to make the Windows 10 batch file work?

Finally, I wonder if there is any way to determine the browser page is complete? (I had to add a sleep() call)

Roger
--
Antiprism Site: http://www.antiprism.com
---
You received this message because you are subscribed to the Google Groups "antiprism" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antiprism+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/antiprism/84889639-3a4d-4a63-84da-bf31c902914fn%40googlegroups.com.

Wagyx Xygaw

unread,
May 14, 2023, 2:10:45 PM5/14/23
to anti...@googlegroups.com
Hi Roger,

Conda is a useful framework to develop in Python.
It allows to create python installations separate from your operating system where you can choose the versions of python and other packages. They are called environments.
It also provides an alternative packet manager from pip.
With Anaconda the python installation comes with dozens of the most used packages in the community (numpy, scipy, pandas, pytorch, ...)
With Miniconda, the installation is kept to its bare minimum for python to work and you can then add the packages that suit your needs.

For instance, on my computer, I have 5 environments with Python versions 3.6, 3.8, 3.9 for different projects. Activating an environment changes the path of the default python.

The fact that you don't need to call python in the command line is because you have "#!/usr/bin/python3" at the beginning of your script.
In my case, it is the terminal with the activated python environment that decides which python I am working with.
Just using "#!/usr/bin/python.exe" might work then, but I have not tried it.
Neither have I tried the windows batch file (it seems to rely on cygwin and I have not it installed).

Regarding the loading of the browser page, since the reading of the file is done asynchronously, I guess you would need to listen to a particular event from the browser.
I have no idea how to do it though.

I don't know if opening a local server on your machine in whatever directory is safe. I think it would only be accessible from your local network.
You could set your firewall to control and deny any access through ports but I don't know more than the internet on this topic.


You received this message because you are subscribed to a topic in the Google Groups "antiprism" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antiprism/kTJyKE8KEoQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antiprism+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/antiprism/15c5f2af-2779-76d9-e0db-b32cc7793337%40interocitors.com.

Roger Kaufman

unread,
May 14, 2023, 8:57:09 PM5/14/23
to anti...@googlegroups.com
Hi Wagyx,

On 5/14/2023 2:10 PM, Wagyx Xygaw wrote:

> I had to had added python.exe in the command line to run the server
> proc = subprocess.Popen([r"python.exe", 'webview_server.py', str(port)])

> The fact that you don't need to call python in the command line is
> because you have "#!/usr/bin/python3" at the beginning of your script.
> In my case, it is the terminal with the activated python environment
> that decides which python I am working with.
> Just using "#!/usr/bin/python.exe" might work then, but I have not
> tried it.
> Neither have I tried the windows batch file (it seems to rely on
> cygwin and I have not it installed).

On my installation, to add the python executable, it had to be like this
which includes full paths. I will rather make a note in the readme about
your situation and leave the line of code simple as it was.

proc = subprocess.Popen(['/cygdrive/c/cygwin64/bin/python
/cygdrive/c/cygwin64/home/roger/bin/webview_server.py', str(port)],
shell=True)

> I don't know if opening a local server on your machine in whatever
> directory is safe. I think it would only be accessible from your local
> network.
> You could set your firewall to control and deny any access through
> ports but I don't know more than the internet on this topic.

I will see if, when the port is open, that I have access to my computer
via a laptop on my same router. That would be the answer.

One question I have is, if you use the original directory and file, how
did you deal with stdin? By always copying to a temporary file, I
avoided any special code for input by pipe. The url must have a file
name as a parameter, so I made it a fixed file name.

Roger

Roger Kaufman

unread,
May 17, 2023, 8:02:44 PM5/17/23
to anti...@googlegroups.com
Hi Wagyx,

On 5/14/2023 8:57 PM, Roger Kaufman wrote:
> One question I have is, if you use the original directory and file,
> how did you deal with stdin? By always copying to a temporary file, I
> avoided any special code for input by pipe. The url must have a file
> name as a parameter, so I made it a fixed file name.

I'm not sure you saw this question but I'm curious about it.

I set it up to use a working directory before I put in argparse. I just
turned out that argparse can do...

parser.add_argument('off_file', nargs='?', type=argparse.FileType('r'),
default=sys.stdin,
                    help='OFF file. can also be from standard input')

so then as a byproduct, standard input would also be copied into the
temporary file.

fout.writelines(args.off_file.readlines())

This seems so compact and easy I don't want to change it from doing the
copy instead of using the original file.

That is why I'd like to know if it can work from standard input for you.

Roger

Roger Kaufman

unread,
May 20, 2023, 3:05:28 PM5/20/23
to anti...@googlegroups.com
Hi Adrian,

I'm about to release version 1.1 of webview. I include an icon file
antiprism.png

Is this ok? I've also been using it for the online viewer tab but it
specifically says "antiprism" in the help.

Roger
offview_tab.jpg

Adrian Rossiter

unread,
May 21, 2023, 1:31:44 AM5/21/23
to anti...@googlegroups.com
Hi Roger
I don't mind if you want to identify it as an associated project, like
I do with antiprism_python, but as it is a separate project it could
also have its own identity.

I notice that antiprism_python is only mentioned on the Antiprism
site on the downloads page, and nowhere in the Antiprism documentation.
It woukd be helpful to have a prominent link on the Programs page at
Links to Complementary Programs / Using OFF format, and webview could
also be included there,

Adrian.
--
Adrian Rossiter
adr...@antiprism.com
http://antiprism.com/adrian

Roger Kaufman

unread,
May 21, 2023, 1:33:51 PM5/21/23
to anti...@googlegroups.com
Hi Adrian,

On 5/21/2023 1:31 AM, Adrian Rossiter wrote:
> I don't mind if you want to identify it as an associated project, like
> I do with antiprism_python, but as it is a separate project it could
> also have its own identity.

I decided last evening I would give webview and the online viewer on my
site a matching icon.

I worked with the webview.desktop with the aim of not needing it to be
customized. The desktop file has an icon field

[Desktop Entry]
Type=Application
Name=Webview
MimeType=model/x-geomview-off;
Exec=bash -c "webview.py %f"
Icon=webview
Terminal=false

Installed in the .local/share/applications folder, with this command
(assumes webview.py is in the path)

~/.local/share/applications$ desktop-file-install webview.desktop --dir
./local/share/applications --rebuild-mime-info-cache

The command warns if Icon has a filetype. Then the icon (the file has
.ico as a filetype) is place in

./local/share/icons

But the only use for the icon here is that it shows up in the "open
with" list for an off file.

The icon set for the mime type is different, and it is also in
./local/share/icons as

model-x-geomview-off.png

I personally use the antiprism icon for this.

>
> I notice that antiprism_python is only mentioned on the Antiprism
> site on the downloads page, and nowhere in the Antiprism documentation.
> It woukd be helpful to have a prominent link on the Programs page at
> Links to Complementary Programs / Using OFF format, and webview could
> also be included there,

I will need to make a web page for this when its done. I have to put the
zip file on my google drive because
python files are executable. I'm unable to download it via a browser or
send it in email.

The last thing I need to tackle is arrays of strings. I have a few
browser directories and I'd like to make this
a variable in the command rather than having to comment/un-comment.
There may be bound conditions etc.
Once I have that it will be more or less done save for any bugs found.

Roger
Reply all
Reply to author
Forward
0 new messages