WebGear API Advanced Usage:¶
This is a continuation of the WebGear doc ➶. Thereby, It's advised to first get familiarize with this API, and its requirements.
After going through following Usage Examples, Checkout more bonus examples here ➶
Using WebGear with Variable Colorspace¶
WebGear by default only supports "BGR" colorspace frames as input, but you can use jpeg_compression_colorspace
string attribute through its options dictionary parameter to specify incoming frames colorspace.
Let's implement a bare-minimum example using WebGear, where we will be sending GRAY frames to client browser:
New in v0.2.2
This example was added in v0.2.2
.
This example works in conjunction with Source ColorSpace manipulation for VideoCapture Gears ➶
Supported jpeg_compression_colorspace
colorspace values are RGB
, BGR
, RGBX
, BGRX
, XBGR
, XRGB
, GRAY
, RGBA
, BGRA
, ABGR
, ARGB
, CMYK
. More information can be found here ➶
And that's all, Now you can see output at http://localhost:8000/
address on your local machine.
Using WebGear with a Custom Source(OpenCV)¶
New in v0.2.1
This example was added in v0.2.1
.
WebGear allows you to easily define your own custom Source that you want to use to transform your frames before sending them onto the browser.
JPEG Frame-Compression and all of its performance enhancing attributes are disabled with a Custom Source!
Let's implement a bare-minimum example with a Custom Source using WebGear API and OpenCV:
And that's all, Now you can see output at http://localhost:8000/
address.
Using WebGear with Custom Mounting Points¶
With our highly extensible WebGear API, you can add your own mounting points, where additional files located, as follows:
Then you can use this folder in your HTML page, to host data-files. For example, if we have jQuery script jquery-3.3.1.slim.min.js
in this folder and want to integrate it, then, we can do something like this:
Using WebGear with Custom Webpage Routes¶
With Webgear's flexible API, you can even add your additional HTML Static webpages without any extra efforts.
Suppose we want to add a simple hello world
webpage to our WebGear server. So let's create a bare-minimum hello.html
file with HTML code as follows:
<html>
<header>
<title>This is Hello world page</title>
</header>
<body>
<h1>Hello World</h1>
<p>how ya doing?</p>
</body>
</html>
Then in our application code, we can integrate this webpage route, as follows:
http://localhost:8000/hello
address.
Using WebGear with MiddleWares¶
WebGear natively supports ASGI middleware classes with Starlette for implementing behavior that is applied across your entire ASGI application easily.
New in v0.2.2
This example was added in v0.2.2
.
All supported middlewares can be found here ➶
For this example, let's use CORSMiddleware
for implementing appropriate CORS headers to outgoing responses in our application in order to allow cross-origin requests from browsers, as follows:
The default parameters used by the CORSMiddleware implementation are restrictive by default, so you'll need to explicitly enable particular origins, methods, or headers, in order for browsers to be permitted to use them in a Cross-Domain context.
Starlette provides several arguments for enabling origins, methods, or headers for CORSMiddleware API. More information can be found here ➶
http://localhost:8000
address.
Rules for Altering WebGear Files and Folders¶
WebGear gives us complete freedom of altering data files generated in Auto-Generation Process, But you've to keep the following rules in mind:
Rules for Altering Data Files¶
- You allowed to alter/change code in all existing default downloaded files at your convenience without any restrictions.
- You allowed to delete/rename all existing data files, except remember NOT to delete/rename three critical data-files (i.e
index.html
,404.html
&500.html
) present intemplates
folder inside thewebgear
directory at the default location, otherwise, it will trigger Auto-generation process, and it will overwrite the existing files with Server ones. - You're allowed to add your own additional
.html
,.css
,.js
, etc. files in the respective folders at the default location and custom mounted Data folders.
Rules for Altering Data Folders¶
- You're allowed to add/mount any number of additional folder as shown in this example above.
- You're allowed to delete/rename existing folders at the default location except remember NOT to delete/rename
templates
folder in thewebgear
directory where critical data-files (i.eindex.html
,404.html
&500.html
) are located, otherwise, it will trigger Auto-generation process.
Bonus Examples¶
Checkout more advanced WebGear examples with unusual configuration here ➶