<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vagiz Duseev</title>
    <description>The latest articles on DEV Community by Vagiz Duseev (@vduseev).</description>
    <link>https://dev.to/vduseev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F609362%2Fa47f857e-b7d4-4ecd-9543-a94012bdcfe9.jpeg</url>
      <title>DEV Community: Vagiz Duseev</title>
      <link>https://dev.to/vduseev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vduseev"/>
    <language>en</language>
    <item>
      <title>Introducing Wutch: python-based Live Server</title>
      <dc:creator>Vagiz Duseev</dc:creator>
      <pubDate>Tue, 06 Apr 2021 09:40:21 +0000</pubDate>
      <link>https://dev.to/vduseev/introducing-wutch-4ii0</link>
      <guid>https://dev.to/vduseev/introducing-wutch-4ii0</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8O_rfuKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl2kidh135qiqjlddn6o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8O_rfuKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl2kidh135qiqjlddn6o.gif" alt="Wutch Demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wutch is a python based live server that observes changes in the given directories and executes a shell command on each change. It also, optionally, renders the results in the default web browser, automatically refreshing each web page after every change.&lt;/p&gt;

&lt;p&gt;You can use wutch with Sphinx, Jekyll, and other static site generators. On the GIF above you can see how wutch builds its own documentation. It behaves just like a live server. Adding any change causes a subprocess shell to run Sphinx (doc site generator used by wutch) to rebuild its docs in real time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why wutch?
&lt;/h3&gt;

&lt;p&gt;If you've ever worked with live servers popular among frontend developers you know how convenient they are. Problem is you should either rely on the Live Server extension such as &lt;a href="https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer"&gt;this one&lt;/a&gt; or create a build pipeline using something like &lt;code&gt;gulp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We wanted to create a command line tool that is able to rebuild documentation or website on each change and automatically refresh browser's webpage afterwards.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Watches multiple directories and glob file patters for changes&lt;/li&gt;
&lt;li&gt;Able to ignore multiple directories and glob file patters&lt;/li&gt;
&lt;li&gt;Runs given shell command on each change&lt;/li&gt;
&lt;li&gt;Starts a live server (with an option to bind to a given port and address)&lt;/li&gt;
&lt;li&gt;(optionally) Opens a browser when Wutch starts pointing to the build directory&lt;/li&gt;
&lt;li&gt;Automatically refreshes web page on each build&lt;/li&gt;
&lt;li&gt;Configurable timeout between rebuilds&lt;/li&gt;
&lt;li&gt;Configurable binging port and host (can be used as a development web server)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Hot to install wutch?
&lt;/h3&gt;

&lt;p&gt;At the moment, wutch is distributed as a Python package for Python &amp;gt;= 3.8. In the future a Homebrew formula, as well as &lt;code&gt;.deb&lt;/code&gt; and &lt;code&gt;.rpm&lt;/code&gt; distributions will be added.&lt;/p&gt;

&lt;p&gt;You can install wutch like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install wutch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, to install for the current user only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install --user wutch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to use wutch?
&lt;/h3&gt;

&lt;p&gt;After installation &lt;code&gt;wutch&lt;/code&gt; binary becomes available. Now you can start a wutch watcher process. By default, it will do the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;watch current directory &lt;code&gt;./&lt;/code&gt; for changes&lt;/li&gt;
&lt;li&gt;start a live server&lt;/li&gt;
&lt;li&gt;open a web browser pointing to the &lt;code&gt;./_build/index.html&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;sphinx-build&lt;/code&gt; shell command on each change&lt;/li&gt;
&lt;li&gt;auto-refresh web page on each build
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ wutch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to configure wutch to perform different actions? Wutch supports multiple sources of configuration settings. They are loaded in a priority given below. Let's take a &lt;code&gt;command&lt;/code&gt; parameter that specifies what shell command to run as an example here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;command line options&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ wutch --command "bundle exec jekyll build"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;environment variables starting with &lt;code&gt;WUTCH_&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;export WUTCH_COMMAND="bundle exec jekyll build"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;configuration file &lt;code&gt;wutch.cfg&lt;/code&gt; present in the current directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;{ "command": "bundle exec jekyll build" }&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;default variables hardcoded into &lt;code&gt;wutch&lt;/code&gt; itself&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sphinx-build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Below is the full list of &lt;strong&gt;supported parameters&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-h, --help            show this help message and exit
-c COMMAND, --command COMMAND
                      Shell command executed in response to file changes. Defaults to: sphinx-build.
-p [PATTERNS ...], --patterns [PATTERNS ...]
                      Matches paths with these patterns (separated by ' '). Defaults to: ['*'].
-P [IGNORE_PATTERNS ...], --ignore-patterns [IGNORE_PATTERNS ...]
                      Ignores file changes in these patterns (separated by ' '). Defaults to: [].
-d [DIRS ...], --dirs [DIRS ...]
                      Directories to watch (separated by ' '). Defaults to: ['.'].
-D [IGNORE_DIRS ...], --ignore-dirs [IGNORE_DIRS ...]
                      Ignore file changes in these directories (separated by ' '). Defaults to: ['_build', 'build'].
-w WAIT, --wait WAIT  Wait N seconds after the command is finished before refreshing the web page. Defaults to: 3.
-b BUILD, --build BUILD
                      Build directory containing files to render in the browser. Defaults to: _build.
-I [INJECT_PATTERNS ...], --inject-patterns [INJECT_PATTERNS ...]
                      Patterns of files to inject with JS code that refreshes them on rebuild (separated by ' '). Defaults to: ['*.htm*'].
-i INDEX, --index INDEX
                      File that will be opened in the browser with the start of the watcher. Defaults to: index.html.
--host HOST           Host to bind internal HTTP server to. Defaults to: localhost.
--port PORT           TCP port to bind internal HTTP server to. Defaults to: 5010.
-B NO_BROWSER, --no-browser NO_BROWSER
                      Do not open browser at wutch launch. Defaults to: False.
-S NO_SERVER, --no-server NO_SERVER
                      Do not start the webserver, just launch the shell command. Defaults to: False.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Building Sphinx documentation with Wutch
&lt;/h4&gt;

&lt;p&gt;Wutch relies on itself when building and rendering its own documentation written with Sphinx (see &lt;a href="https://github.com/vduseev/wutch/tree/master/docs"&gt;wutch docs sources&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In order to set up a Sphinx doc development using wutch's live server it's enough to place a &lt;code&gt;wutch.cfg&lt;/code&gt; file into the project folder and then simply run wutch binary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "dirs": ["docs"],
    "patterns": ["*.rst", "*.py"],
    "command": "make -C docs build",
    "build": "docs/_build/html",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With these settings wutch will look for the changes happening in &lt;code&gt;*.rst&lt;/code&gt; and &lt;code&gt;*.py&lt;/code&gt; files in the &lt;code&gt;./docs&lt;/code&gt; directory. For each change, wutch will run &lt;code&gt;make -C docs build&lt;/code&gt; command from its &lt;a href="https://github.com/vduseev/wutch/blob/master/docs/Makefile"&gt;Makefile&lt;/a&gt;. It will then open the &lt;code&gt;./docs/_build/html/index.html&lt;/code&gt; file in the default browser and will auto-refresh the browser page every rebuild.&lt;/p&gt;

&lt;h4&gt;
  
  
  Building Jekyll with Wutch
&lt;/h4&gt;

&lt;p&gt;Jekyll already comes with the &lt;code&gt;jekyll serve&lt;/code&gt; command that creates a server which rebuilds the website for every change. However, jekyll is not able to refresh the page in the browser automatically.&lt;/p&gt;

&lt;p&gt;Imagine you have a pretty standard Jekyll project structure. Below is a structure of my blog.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drwxr-xr-x  _drafts
drwxr-xr-x  _includes
drwxr-xr-x  _layouts
drwxr-xr-x  _posts
drwxr-xr-x  _site
drwxr-xr-x  assets
-rwxr-xr-x  index.md
drwxr-xr-x  pages
-rw-r--r--  robots.txt
-rwxr-xr-x  404.html
-rwxr-xr-x  Gemfile
-rw-r--r--  Gemfile.lock
-rwxr-xr-x  _config.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is how to build and run a live server for it with Wutch.&lt;br&gt;
For this example we will be using command line options instead of the config file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ wutch --ignore-dirs _site --build _site --command "bundle exec jekyll build"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using Wutch as live server for frontend builds
&lt;/h4&gt;

&lt;p&gt;Even though there are plenty of options in frontend to implement a live server that watches for changes and rebuilds CSS and HTML, still, wutch is capable of that too.&lt;/p&gt;

&lt;p&gt;Imagine you have a Gulp based build pipeline for your website and you can run a build using &lt;code&gt;gulp build&lt;/code&gt; command. Then all results of the build are dropped into the &lt;code&gt;build&lt;/code&gt; directory of the project.&lt;/p&gt;

&lt;p&gt;In that case we'd need to look for the changes in the current directory, but ignore the changes in the &lt;code&gt;build&lt;/code&gt; folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ wutch --ignore-dirs "build" --build "build" --command "gulp build"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  More about Wutch
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Documentation: &lt;a href="https://wutch.readthedocs.io"&gt;wutch.readthedocs.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub repo: &lt;a href="https://github.com/vduseev/wutch"&gt;github.com/vduseev/wutch&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PyPI package: &lt;a href="https://pypi.org/project/wutch/"&gt;pypi.org/project/wutch&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>showdev</category>
      <category>liveserver</category>
      <category>shell</category>
    </item>
  </channel>
</rss>
