Easy local saving with Python

925 views
Skip to first unread message

UBi

unread,
Dec 28, 2019, 11:52:43 AM12/28/19
to TiddlyWiki
Hello,

I had some spare time recently, so I tried to re-implement the Ruby Server in Python:

#!/usr/bin/python3

from http.server import SimpleHTTPRequestHandler, HTTPServer
import datetime, shutil, os

def makebackup(src):
   
(srcpath, srcfile) = os.path.split(src)
    tstamp
=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    dstpath
= os.path.join(srcpath,'twBackups')
   
if not os.path.exists(dstpath):
        os
.mkdir(dstpath)
    shutil
.copyfile(src, os.path.join(dstpath, srcfile+'.'+tstamp))

class ExtendedHandler(SimpleHTTPRequestHandler):
   
def do_OPTIONS(self):
       
self.send_response(200, 'OK')
       
self.send_header('allow','GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav')
       
self.send_header('x-api-access-type','file')
       
self.send_header('dav','tw5/put')
       
self.end_headers()
   
def do_PUT(self):
        length
= int(self.headers['Content-Length'])
        path
= self.translate_path(self.path)
        makebackup
(path)
       
with open(path, "wb") as dst:
            dst
.write(self.rfile.read(length))
       
self.send_response(200, 'OK')
       
self.send_header('Content-Type', 'text/html')
       
self.end_headers()

os
.chdir(os.path.dirname(os.path.abspath(__file__)))
HTTPServer(('localhost',8080),ExtendedHandler).serve_forever()

It works for me :-)

Mohammad

unread,
Dec 28, 2019, 1:32:24 PM12/28/19
to TiddlyWiki
Hi UBI,
 Would you please add some description with one example to let us how to use it for saving Tiddlywiki

--Mohammad

UBi

unread,
Dec 28, 2019, 5:00:42 PM12/28/19
to TiddlyWiki
I will describe my use case on Linux, Windows should work in a similar fashion.

My TiddlyWiki files live in /home/ubi/TW, abbreviated ~/TW. There I placed the script as twserver.py.
For a first test, I started it manually in a terminal window:

/usr/bin/python3 ~/TW/twserver.py

Then I pointed my browser to http://localhost:8080/. Status messages started appearing in the terminal window.

In the browser window a list of files and directories below ~/TW appeared.

I opened one of my TW files, notes.html, and created a new Tiddler. The I saved the changes.
This
1) backed up ~/TW/notes.html html to ~/TW/twBackups/notes.html.YYYYMMDDhhmmss, creating ~/TW/twBackups on the fly.
2) saved the changes to ~/TW/notes.html.

Now I have to find out how / where I can add a call to the script to my startup or login procedures.

HTH UBi


Les Farrell

unread,
Dec 28, 2019, 5:34:31 PM12/28/19
to tiddl...@googlegroups.com
This seems to work fine on Windows, I'm not a python programmer but adding the line.

os.system('"C:/Program Files/Firefox Developer Edition/firefox.exe" -url http://127.0.0.1:8080/mytiddlywiki.html')

before

HTTPServer(('localhost',8080),ExtendedHandler).serve_forever()

Seems to starts up firefox pointing to my wiki without problem. (I'm using the Developer Edition of firefox so your path might be different!)

Thanks UBi

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/1a748dec-a911-478e-ba49-647c0c6a4e5b%40googlegroups.com.

Mohammad

unread,
Dec 29, 2019, 12:56:18 AM12/29/19
to TiddlyWiki
I tested on Windows 10 and Python 3.7.5 and it works like a charm!

This is really simple! We know Python is quite popular and many people use Python in their work! So this open new opportunities for using
Tiddlywiki with Python projects!

Some minor comments:
 1. add date stamp before .html extension
 2. let to define multi path for wikis (right now anything should be in the same directory or sub directories of twserver.py
 3. you may announce it in https://pypi.org/

Thank you again for this nifty tool.

--Mohammad

Mohammad

unread,
Dec 29, 2019, 2:10:02 AM12/29/19
to TiddlyWiki
The script works fine, I am just curious about the message 404 appears on terminal window. I have highlighted them.

Mohammad@6600K C:\TW\201. Python server                                       
$ python server.py                                                            
127.0.0.1 - - [29/Dec/2019 09:14:54] "GET / HTTP/1.1" 200 -                   
127.0.0.1 - - [29/Dec/2019 09:14:54] code 404, message File not found         
127.0.0.1 - - [29/Dec/2019 09:14:54] "GET /favicon.ico HTTP/1.1" 404 -        
127.0.0.1 - - [29/Dec/2019 09:15:37] "GET /wikis/ HTTP/1.1" 200 -             
127.0.0.1 - - [29/Dec/2019 09:15:40] "GET /wikis/tw.html HTTP/1.1" 200 -      
127.0.0.1 - - [29/Dec/2019 09:15:41] "OPTIONS /wikis/tw.html HTTP/1.1" 200 -  
127.0.0.1 - - [29/Dec/2019 09:15:42] code 404, message File not found         
127.0.0.1 - - [29/Dec/2019 09:15:42] "GET /wikis/favicon.ico HTTP/1.1" 404 -  
127.0.0.1 - - [29/Dec/2019 09:16:16] "PUT /wikis/tw.html HTTP/1.1" 200 -      
127.0.0.1 - - [29/Dec/2019 09:16:16] "HEAD /wikis/tw.html HTTP/1.1" 200 -     
127.0.0.1 - - [29/Dec/2019 09:41:05] "GET / HTTP/1.1" 200 -                   
127.0.0.1 - - [29/Dec/2019 09:41:06] code 404, message File not found         
127.0.0.1 - - [29/Dec/2019 09:41:06] "GET /favicon.ico HTTP/1.1" 404 -        



On Sunday, December 29, 2019 at 1:30:42 AM UTC+3:30, UBi wrote:

Mohammad

unread,
Dec 29, 2019, 2:44:37 AM12/29/19
to TiddlyWiki


On Sunday, December 29, 2019 at 1:30:42 AM UTC+3:30, UBi wrote:

Mohammad

unread,
Dec 29, 2019, 2:45:32 AM12/29/19
to TiddlyWiki
More comments:
  • add a line for terminating the code
  • on windows ctrl+C works with a not good exit message

On Saturday, December 28, 2019 at 8:22:43 PM UTC+3:30, UBi wrote:

UBi

unread,
Dec 29, 2019, 6:16:38 AM12/29/19
to TiddlyWiki
Hi Mohammad,

as you can see on the line following the yellow lines, your browser automatically tries to fetch a favicon.ico file.
If there is none, the server sends a 404 error code, usually accompanied by a short description.
It seems that in this case resolving the code to a description went wrong.
I can't tell you why, I'm not that familiar with the SimpleHTTPRequestHandler innards.

UBi

unread,
Dec 29, 2019, 7:02:18 AM12/29/19
to TiddlyWiki
Argl. According to https://en.wikipedia.org/wiki/HTTP_404, 404 means "Not found".
Thus we could read the yellow lines as

code: 404, message "File not found"

This fits the missing favicon.ico situation.

Mohammad

unread,
Dec 29, 2019, 10:06:02 AM12/29/19
to TiddlyWiki
Hi UBi
 Many thanks for your clarification!
Python Server worth to be added to Tiddlywiki.com tiddler on saving and working with Tiddlywiki.
Cheers
Mohammad

UBi

unread,
Dec 29, 2019, 1:15:16 PM12/29/19
to TiddlyWiki
Hi again,

here is a slightly improved version of twserver.py (moved timestamp before .html in backup file names, tidied code).

#!/usr/bin/python3

import datetime, shutil, os, http.server

def makebackup(src):
   
(srcpath, srcfile) = os.path.split(src)

   
(srcname, src_ext) = os.path.splitext(srcfile)

    tstamp
=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    dstpath
= os.path.join(srcpath,'twBackups')
   
if not os.path.exists(dstpath):
        os
.mkdir(dstpath)

    shutil
.copyfile(src, os.path.join(dstpath, srcname+'-'+tstamp+src_ext))

class ExtendedHandler(http.server.SimpleHTTPRequestHandler):

   
def do_OPTIONS(self):
       
self.send_response(200, 'OK')
       
self.send_header('allow','GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav')
       
self.send_header('x-api-access-type','file')
       
self.send_header('dav','tw5/put')
       
self.end_headers()
   
def do_PUT(self):

        path
= self.translate_path(self.path)
        makebackup
(path)
       
with open(path, "wb") as dst:

            dst
.write(self.rfile.read(int(self.headers['Content-Length'])))
       
self.send_response(200, 'OK')
       
self.end_headers()

os
.chdir(os.path.dirname(os.path.abspath(__file__)))
http
.server.HTTPServer(('localhost',8080),ExtendedHandler).serve_forever()

Like it's template, https://tiddlywiki.com/#Saving%20via%20a%20Minimal%20Ruby%20Server, it covers only the bare minimum TW server needs.
Feel free to use it / customise it at will.

Mohammad

unread,
Dec 29, 2019, 1:29:59 PM12/29/19
to TiddlyWiki
Good improvements
Thanks for the update!

--Mohammad

TonyM

unread,
Dec 29, 2019, 5:37:29 PM12/29/19
to TiddlyWiki
Folks

Can we include a simple overview for people not familiar with python and its environment? I presume you can get it for multiple os implementations and make use of it on most platforms. Does it have defaults that may clash with other local hosts or specific ports etc... Basicaly we need to provide the information people need to select it without first installing it.

Regards
Tony

UBi

unread,
Dec 30, 2019, 6:05:14 AM12/30/19
to tiddl...@googlegroups.com
Do you mean something like this?
  • Python is a widely used multi purpose programming/scripting language available for free.
  • Linux: on most Linux distributions Python is pre-installed or can be added easily with the distribution specific package manager.
  • Windows: download it from https://www.python.org and install it. You can safely accept the default install options. It eats up some of your disk space, adds new entries to your start menu, but has no impact otherwise. Except that you now can create and/or run Python programs, twserver.py for example.
UBi

Mohammad

unread,
Dec 30, 2019, 8:01:28 AM12/30/19
to TiddlyWiki
To David Gifford,

Please add Python Server to Tiddlywiki toolmap! It is a single html file saver

--Mohammad

Mohammad

unread,
Dec 30, 2019, 10:59:49 AM12/30/19
to TiddlyWiki
Hi Tony!
 Python is now available through MS stote


So, Microsoft Windows 10 has features to get Python installed very easily!

If you are familiar with Python then you absolutely knows better than MS which version fits your case!

--Mohammad

Mark S.

unread,
Dec 30, 2019, 12:10:22 PM12/30/19
to TiddlyWiki
Added under saving.

Panos Firbas

unread,
Dec 30, 2019, 2:55:51 PM12/30/19
to TiddlyWiki
Hi all,    
     
I fiddled with the script a bit today for my personal use, and since this is a hot thread, I'm back to share my little improvements with you.     
I'm very new to tiddly so please advise if I should share these things somewhere else.     
        
In the tiddlyserver script, I added a function to cleanup the backedup files. It keeps the last X files (Default=5) and is triggered right after a backup is made.
I also added https functionality.        

Please note that I also made a slight change in the folder structure, I keep things at /home/user/tiddlyserver/. In there, we have the twBackups folder,
the tiddlyserver.py script, and a /served folder which contains the .html files to be served. The motivation was to not serve the script itself, or the backups.     
         
You can find the updated script here (use at your own risk, of course):
    

I also made a .service script to make this server a systemctl service for linux systems that use it.       
You'll find instructions in the script itself, I don't know much about these but it seems to be working like a charm on my vps      

Hope you find it useful,       
-- Panos

Mohammad

unread,
Dec 30, 2019, 4:26:54 PM12/30/19
to TiddlyWiki


On Monday, December 30, 2019 at 11:25:51 PM UTC+3:30, Panos Firbas wrote:
Hi all,    
     
I fiddled with the script a bit today for my personal use, and since this is a hot thread, I'm back to share my little improvements with you.     
I'm very new to tiddly so please advise if I should share these things somewhere else.     

I think this is the right place, but you can also share on  https://groups.google.com/forum/#!forum/tiddlywikidev
        
In the tiddlyserver script, I added a function to cleanup the backedup files. It keeps the last X files (Default=5) and is triggered right after a backup is made.
I also added https functionality.        


This is very nice feature! I may recommend to have an option file as a json or simpler one to read the settings. These are backup folder, and other options
I think user with no or little Python knowledge can use this settings file easier.
 
Please note that I also made a slight change in the folder structure, I keep things at /home/user/tiddlyserver/. In there, we have the twBackups folder,
the tiddlyserver.py script, and a /served folder which contains the .html files to be served. The motivation was to not serve the script itself, or the backups. 

As there is a crowd of Windows user, as I explained above the backup folder can be set by user in a simple way!

Thank you for sharing and please let us know your improvements.

--Mohammad

TiddlyTweeter

unread,
Dec 30, 2019, 5:31:05 PM12/30/19
to TiddlyWiki
Mohammad wrote:
As there is a crowd of Windows user, as I explained above the backup folder can be set by user in a simple way!

What I find interesting is that once installed Python is quite easy to configure on Windows, as far as I can see.

For Windows users I'm wondering if we could package together a python system and TiddlyWikis as a bundle??

Seamless install of wiki with built in save is a bit like the Holy Grail, but I think worth mentioning that ultimately we want people to use TW in the easiest "universal" (cross platform) way.
Is this approach close to that?

Could one wrap Python in an installer for Windows and be up and editing TW immediately?

Thoughts
TT

UBi

unread,
Dec 31, 2019, 4:55:22 AM12/31/19
to TiddlyWiki
Hi Panos,

I'm glad that people play around with my script, and adopt it to their needs. I was thinking about a cleanup mechanism, too. Now I can take yours. I like open source :-)

Regarding your implementation, I have two questions:
1) Why do you want the user not to see the backup directory? With it being visible, an user could simply open an archived TiddlyWiki for reference without leaving the browser.
2) What is the benefit of using SSL on localhost?

Regarding automatic startup: I made the script executable (chmod a+x twserver.py), and added it to the Startup Applications in the Mate Control Center (I'm using Debian with Mate Desktop). Your systemctl solution has the advantage of being Desktop Environment agnostic, but might be slightly more complicated to implement (root access).

UBi

UBi

unread,
Dec 31, 2019, 5:07:17 AM12/31/19
to TiddlyWiki
Hi TT,

the idea of packaging a 30+ line script with a 25MB zipped Python distribution mad me chuckle. But then, why not?
This might work as well with the Ruby Server script and a Ruby distribution, to be fair.

UBi

Panos Firbas

unread,
Dec 31, 2019, 6:53:12 AM12/31/19
to TiddlyWiki
Hi UBi,    
      
Regarding the cleanup mechanism, please be advised that it needs to be updated (I realized this last night before falling asleep, I'll be fixing it now): as it is now, it  will delete any backups that STARTSWITH the string of the current file (i.e. if you're working on "empty.html" and there's a previous "emptyXXX.html" backup, the "emptyXXX" will be deleted!! bad design, sorry for that !).       
     
Regarding my choices, the important difference is that I wanted to host my tiddly on a remote machine. This way I can access it from any computer I am on (I have at least 4 machines that I would be wanting to open my notes from, including my phone). So ssl makes things a bit safer, and automatic startup makes it much more hassle free.       
       
Regarding the backups, I was considering the following: maybe we can setup a git-based backup function? Instead of just saving a new backup file, we can overwrite the file in a backup folder and then call a simple git add git commit on that folder? (It's pretty simple in python). This way the size won't balloon and it should be safe to revert to any previous state.
What do you think?      
      
You are right about the backup folder as it is now, not exactly sure why I didn't want to serve it.
        
One more thing I'll be looking into is implementing some sort of rudimentary authentication on the server. If I understand right, anyone who stumbles upon my server's tiddly can change it now.                 
         
Regarding Windows and python ans stuff, maybe we can package the tiddlyserver in a pypi package so that people can just install python and then "pip install tiddlyserver"?      
That should be simple enough, Does windows python come with pip? I assume it would        
           
Cheers, I'll be back later with today's updates

UBi

unread,
Dec 31, 2019, 7:38:12 AM12/31/19
to TiddlyWiki
Hi Panos,

your use case is definitely more network centric than mine. Maybe you should consider the Node.js flavour of TiddlyWiki, and set up a https://tiddlywiki.com/static/WebServer.html? The biggest disadvantage, from my point of view: it's not Python :-)

Donald Coates

unread,
Dec 31, 2019, 8:49:02 AM12/31/19
to tiddl...@googlegroups.com
I tried to push node several times on Reddit he's not biting.  :D  Panos you could just use the authorization of apache/nginx .  But just to push node one more time it does have authorization baked in as well as the ability to show a read only version and use ssl certs.

I do understand the desire to make something work however.  That's why we're here after all!

Mark S.

unread,
Dec 31, 2019, 10:20:09 AM12/31/19
to TiddlyWiki

There is an installer utility (maybe more than one) that will wrap your script into it's own windows executable. The thing is, it won't be certified, so may ring alarm bells. In that sense, it may be easier to install python which (I assume) will have been certified.

TiddlyTweeter

unread,
Dec 31, 2019, 10:32:17 AM12/31/19
to TiddlyWiki
Mark S. wrote:
There is an installer utility (maybe more than one) that will wrap your script into it's own windows executable. The thing is, it won't be certified, so may ring alarm bells. In that sense, it may be easier to install python which (I assume) will have been certified.

Good point. I wonder how difficult it would be to certify a bespoke "package"?

Best wishes
TT

Panos Firbas

unread,
Dec 31, 2019, 11:25:43 AM12/31/19
to TiddlyWiki
Hello all,     
      
Since I keep updating this, I started a little repo:       
       
I changed the backup method to git: every time PUT is called, python makes a system call to git to add and commit the new file.
It's super basic but it works. I think this won't balloon too much, we'll see.        
     
I also implemented a very basic and unsafe user authentication (trigger warning: it stores the key in plain text).     
        
Finally, I put all the script options in a config file to make things a bit cleaner.       
     
Please feel free to fork clone etc. etc.     
     
This is fun!

ps.      
@DonaldCoates : Yep, I like learning and I like avoiding nodejs (call me weird but I think js should stay in the frontend) :)

Mohammad

unread,
Dec 31, 2019, 1:23:10 PM12/31/19
to TiddlyWiki
Hi Panos,
 This is absolutely a great improvement! I have some minor comments:

1. Is it possible on server start launch a wiki to act as the landing page and from there I could click any wiki I like to open
2. How serve folders located on different drive for example C:/TW, D:/myNotes, ...

Please keep going on!

--Mohammad

Mohammad

unread,
Dec 31, 2019, 1:31:32 PM12/31/19
to TiddlyWiki
TT
 Python is supported by Windows 10. You can install it like you install Skype. It is also available on Windows store!
 For PythonServer by Ponas and UBi, the best is to distribute it as pypi!
just pip it and it will be at your access.
Ponas and UBi can add a short paragraph to teach who are not familiar with Python.

--Mohammad

Mohammad

unread,
Dec 31, 2019, 1:34:43 PM12/31/19
to TiddlyWiki
This thread seems very hot and attracted many! In one day 216 view is a record!
So, it seems many people like Python+Tiddlywiki

@Ponas and UBi
Inform Jeremy (through a PR) to add Python Server to tiddlywiki.com



--Mohammad

On Tuesday, December 31, 2019 at 7:55:43 PM UTC+3:30, Panos Firbas wrote:

Panos Firbas

unread,
Dec 31, 2019, 1:58:20 PM12/31/19
to tiddl...@googlegroups.com
Hey Mohammad,    
     
> Is it possible on server start launch a wiki to act as the landing page and from there I could click any wiki I like to open     
       
If you have a wiki named "index.html" in the served folder, the python server will automatically launch that instead of the directory view.     
The downside is that as far as I know, you cannot then revert to the directory view so you will have to manually go to any other wiki.      
        
You would then have to maintain links to your other wikis in your main wiki. I'll add a note of this in the repo.     
          
> How serve folders located on different drive for example C:/TW, D:/myNotes, ...

Hmmmm, I could change the served folder to be a parameter in the configuration file instead of defaulting to ./served, this way the server could easily serve any single folder.       
I'd have to also make sure that git is initialized in gitless folders but that shouldn't be too hard.     
      
Serving multiple folders sounds a bit more challenging... I could symlink a bunch of folders into one, but then I'm not sure how to handle the git issue.       
                  
With regards to windows support, I personally don't have a windows machine so I can't do too much trial and error. I'd be happy to package this once it's a bit more stable,
but I think for now copy pasting the files and folder structure should be easier for everyone. (git is available on windows right? They would also need that for my version)


ps.     
You got my name right the first time, it's Panos :P

TiddlyTweeter

unread,
Dec 31, 2019, 2:00:12 PM12/31/19
to TiddlyWiki
I think what is attractive is the apparent ease of it.

I found it simple to get running on Windows. 
Much easier than other solutions i have tried.

I'd say it is definitely something to pursue further. 

Best wishes
TT

Mohammad

unread,
Dec 31, 2019, 2:09:05 PM12/31/19
to TiddlyWiki
Hi Panos,


On Tuesday, December 31, 2019 at 10:28:20 PM UTC+3:30, Panos Firbas wrote:
Hey Mohammad,    
     
> Is it possible on server start launch a wiki to act as the landing page and from there I could click any wiki I like to open     
       
If you have a wiki named "index.html" in the served folder, the python server will automatically launch that instead of the directory view.     
The downside is that as far as I know, you cannot then revert to the directory view so you will have to manually go to any other wiki.      
        
You would then have to maintain links to your other wikis in your main wiki. I'll add a note of this in the repo.     
          
> How serve folders located on different drive for example C:/TW, D:/myNotes, ...
 

Hmmmm, I could change the served folder to be a parameter in the configuration file instead of defaulting to ./served, this way the server could easily serve any single folder.       
I'd have to also make sure that git is initialized in gitless folders but that shouldn't be too hard.     
      
Serving multiple folders sounds a bit more challenging... I could symlink a bunch of folders into one, but then I'm not sure how to handle the git issue.       
                  
With regards to windows support, I personally don't have a windows machine so I can't do too much trial and error. I'd be happy to package this once it's a bit more stable,
but I think for now copy pasting the files and folder structure should be easier for everyone. (git is available on windows right? They would also need that for my version)

Okay, I understood! lets keep it simple so I will put all my wikis in .served folder

There is no git pre-installed on Windows, but it can be easily installed, I will help later if you want to add a windows installation to repo!
Python is seamlessly installed no hassle! It is up to users, but Windows store may be the first choice! I personally prefer python.org distributions
 

ps.     
You got my name right the first time, it's Panos :P
Sorry for that, I was writting from cell phone!

Mohammad

unread,
Dec 31, 2019, 2:12:26 PM12/31/19
to tiddl...@googlegroups.com
Panos,

For me it failed to save using git? [Windows 10 Python 3.7.5]

Question:

In tiddly.service what is the working folder? Do I need to set it on Windows?

[Service]
User=USER
WorkingDirectory=/home/USER/tiddlywiki/
ExecStart=/usr/bin/python3 /home/USER/tiddlywiki/tiddlyserver.py
Restart=always

Mohammad

unread,
Dec 31, 2019, 2:25:07 PM12/31/19
to TiddlyWiki
More comments:

It is good user knows the server has been successfully started

like

print("Tiddlyserver started ...")
theserver.serve_forever()

Produces on Windows 10, Python 3.7.5


Mohammad@N550JK C:\Temp
$ python tiddlyserver
.py
Tiddlyserver started ...

Mohammad

unread,
Dec 31, 2019, 2:44:41 PM12/31/19
to TiddlyWiki
@UBi,

Please put this Python Server somewhere on the net like tiddlyspot and for future reference!
Also I appreciate if you kindly add the description and how to use notes to that wiki!
As TT said your solution for localhost is a very simple to use and understand!
Your idea got a lot of attention!

--Mohammad

Panos Firbas

unread,
Dec 31, 2019, 4:42:42 PM12/31/19
to TiddlyWiki
Hey Muhammad,    
    
The .service file is exclusively for linux systems no need to worry about it on Windows, it will do nothing.        
          
Thinking about it, if you are going to use it only on your local machine (I assume windows users are not looking to serve the wiki on the web),
the features I added are not of much use. UBi's solution would be ideal for that, just plop the snippet in any folder you want to serve and run it.      
           
Git could be a useful feature even for local usage though, and I think I have an idea why it failed, it has to do with the way I call git from inside python
(which of course is different on windows). I'll check for a universal solution tomorrow.              
      
I'll also add some reporting :)

TonyM

unread,
Dec 31, 2019, 8:53:12 PM12/31/19
to TiddlyWiki
UBi

Yes, that is well on the way. We can fill it out based on a little more experience.

Thanks
Tony

Panos Firbas

unread,
Jan 2, 2020, 2:30:19 PM1/2/20
to TiddlyWiki
Hey all,    
     
I made some more updates on the server:      
You can now turn off the extra features (https, user auth, git) from the configuration file, and it can print some reporting on the command line.    

       
      
@TonyM, are you adding a reference in tiddlywiki.com? Should I pm Jeremy myself?


Cheers,
Panos

Mohammad

unread,
Jan 2, 2020, 3:48:10 PM1/2/20
to TiddlyWiki
Hi Panos,
 This is great! Thank you!

p.s: always put the demo and code link here for people to click and open the code or demo page

--Mohammad

Mohammad

unread,
Jan 14, 2020, 10:13:35 AM1/14/20
to TiddlyWiki
Hi Panos,
 Having the latest Python Server with (no authentication, no Git, no https) I get the following error when I push the save button or when TW saves automatically


Exception happened during processing of request from ('127.0.0.1', 56450)
Traceback (most recent call last):
 
File "D:\Python\Python37\lib\socketserver.py", line 316, in _handle_request_noblock
   
self.process_request(request, client_address)
 
File "D:\Python\Python37\lib\socketserver.py", line 347, in process_request
   
self.finish_request(request, client_address)
 
File "D:\Python\Python37\lib\socketserver.py", line 360, in finish_request
   
self.RequestHandlerClass(request, client_address, self)
 
File "D:\Python\Python37\lib\http\server.py", line 646, in __init__
   
super().__init__(*args, **kwargs)
 
File "D:\Python\Python37\lib\socketserver.py", line 720, in __init__
   
self.handle()
 
File "D:\Python\Python37\lib\http\server.py", line 426, in handle
   
self.handle_one_request()
 
File "D:\Python\Python37\lib\http\server.py", line 414, in handle_one_request
    method
()
 
File "tiddlyserver.py", line 91, in do_PUT
    makebackup
(path)
 
File "tiddlyserver.py", line 73, in makebackup
   
if int(self.args.VOCAL) >0:
NameError: name 'self' is not defined



--Mohammad

On Thursday, January 2, 2020 at 11:00:19 PM UTC+3:30, Panos Firbas wrote:

Panos Firbas

unread,
Jan 14, 2020, 10:28:22 AM1/14/20
to TiddlyWiki
Hey Mohammad,      
      
Thank you for trying it out! Well this was embarassing(ly simple to fix).. it should be fixed, would you be kind enough to give it another shot ?
        
Cheers,
Panos

Mohammad

unread,
Jan 14, 2020, 1:21:48 PM1/14/20
to TiddlyWiki
Hi again Panos,

No success. The error has been given below
OS: Win10
Python 3.7

$ python tiddlyserver.py
127.0.0.1 - - [14/Jan/2020 21:50:11] "GET /empty.html HTTP/1.1" 304 -
127.0.0.1 - - [14/Jan/2020 21:50:11] "OPTIONS /empty.html HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49777)
Traceback (most recent call last):
  File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 646, in __init__
    super().__init__(*args, **kwargs)
  File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 426, in handle
    self.handle_one_request()
  File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 414, in handle_one_request
    method()
  File "tiddlyserver.py", line 91, in do_PUT
    makebackup(path)
  File "tiddlyserver.py", line 73, in makebackup
    if int(args.VOCAL) >0:
NameError: name 'args' is not defined


--Mohammad

Panos Firbas

unread,
Jan 17, 2020, 5:40:00 AM1/17/20
to TiddlyWiki
Hi Mohammad,

I found some time today to have another look at it, would you give it another shot please?

Mohammad

unread,
Jan 17, 2020, 6:22:06 AM1/17/20
to TiddlyWiki
Sure,
 I will return to you soon!

--Mohammad

Mohammad

unread,
Jan 17, 2020, 6:30:25 AM1/17/20
to TiddlyWiki
Panos,

 It works fine! Thank you.

- Windows 10 + Python 3.7 + Chrome 79

-Configuration: localhost, no Git, no authentication, no https.


I like to know how it is configured for Git!


--Mohammad

tony

unread,
Jan 21, 2020, 4:37:41 AM1/21/20
to TiddlyWiki
Thank you UBi!

So refreshing to have alternatives.

Tested and works under Debian Linux via ChromeOs in an inexpensive Chromebook running Crostini

and timely given the future status of Chrome Apps potentially affecting TiddlyChrome and/or TiddlyDrive

Best,
tony


On Sunday, December 29, 2019 at 10:15:16 AM UTC-8, UBi wrote:
Hi again,

here is a slightly improved version of twserver.py (moved timestamp before .html in backup file names, tidied code).

#!/usr/bin/python3

import datetime, shutil, os, http.server

def makebackup(src):
   
(srcpath, srcfile) = os.path.split(src)
   
(srcname, src_ext) = os.path.splitext(srcfile)
    tstamp
=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    dstpath
= os.path.join(srcpath,'twBackups')
   
if not os.path.exists(dstpath):
        os
.mkdir(dstpath)
    shutil
.copyfile(src, os.path.join(dstpath, srcname+'-'+tstamp+src_ext))

class ExtendedHandler(http.server.SimpleHTTPRequestHandler):
   
def do_OPTIONS(self):
       
self.send_response(200, 'OK')
       
self.send_header('allow','GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav')
       
self.send_header('x-api-access-type','file')
       
self.send_header('dav','tw5/put')
       
self.end_headers()
   
def do_PUT(self):
        path
= self.translate_path(self.path)
        makebackup
(path)
       
with open(path, "wb") as dst:
            dst
.write(self.rfile.read(int(self.headers['Content-Length'])))
       
self.send_response(200, 'OK')
       
self.end_headers()

os
.chdir(os.path.dirname(os.path.abspath(__file__)))
http
.server.HTTPServer(('localhost',8080),ExtendedHandler).serve_forever()

Like it's template, https://tiddlywiki.com/#Saving%20via%20a%20Minimal%20Ruby%20Server, it covers only the bare minimum TW server needs.
Feel free to use it / customise it at will.

Am Samstag, 28. Dezember 2019 17:52:43 UTC+1 schrieb UBi:
Hello,

I had some spare time recently, so I tried to re-implement the Ruby Server in Python:

#!/usr/bin/python3

from http.server import SimpleHTTPRequestHandler, HTTPServer
import datetime, shutil, os

def makebackup(src):
   
(srcpath, srcfile) = os.path.split(src)
    tstamp
=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    dstpath
= os.path.join(srcpath,'twBackups')
   
if not os.path.exists(dstpath):
        os
.mkdir(dstpath)
    shutil
.copyfile(src, os.path.join(dstpath, srcfile+'.'+tstamp))

class ExtendedHandler(SimpleHTTPRequestHandler):
   
def do_OPTIONS(self):
       
self.send_response(200, 'OK')
       
self.send_header('allow','GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav')
       
self.send_header('x-api-access-type','file')
       
self.send_header('dav','tw5/put')
       
self.end_headers()
   
def do_PUT(self):
        length
= int(self.headers['Content-Length'])
        path
= self.translate_path(self.path)
        makebackup
(path)
       
with open(path, "wb") as dst:
            dst
.write(self.rfile.read(length))
       
self.send_response(200, 'OK')
       
self.send_header('Content-Type', 'text/html')
       
self.end_headers()

os
.chdir(os.path.dirname(os.path.abspath(__file__)))
HTTPServer(('localhost',8080),ExtendedHandler).serve_forever()

It works for me :-)

Nathan Warner

unread,
Sep 26, 2020, 12:31:35 PM9/26/20
to TiddlyWiki
Hi all, wanted to share a python server project I worked on. https://github.com/itsamenathan/tiddlysaver-python

PMario

unread,
Sep 26, 2020, 3:32:52 PM9/26/20
to TiddlyWiki
Hi Nathan,

I think, you should have started your own thread. IMO it deserves it.

have fun!
mario

Gurudeep Rao

unread,
Dec 21, 2020, 5:42:07 AM12/21/20
to TiddlyWiki
@UBI, @Mohammad, 

This might interest you,

Mohammad Rahmani

unread,
Dec 21, 2020, 12:23:02 PM12/21/20
to tiddl...@googlegroups.com
Hi Gurudeep,
Many thanks for sharing. Eel is an amazing tool and has a lot of potential to integrate Tiddlywiki with Python.
I just made some experiments. For me it does not save Tiddlywiki out of the box! 

Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/8f03ef81-c7cc-49fc-b5d3-99dfb7bffe41n%40googlegroups.com.

Gurudeep Rao

unread,
Dec 23, 2020, 4:40:23 AM12/23/20
to tiddl...@googlegroups.com
Hey Mohammad,

Yeah it does not save ootb, but I was trying something to see if we can write a minimal Python module that does it or store the json in TinyDB 

You received this message because you are subscribed to a topic in the Google Groups "TiddlyWiki" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tiddlywiki/M64suMWXDYQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/CAAV1gMAjwmuSSEBcbQYFoZBWbjHQnrhziSha8TqtEoXvDHJrQg%40mail.gmail.com.

Mohammad Rahmani

unread,
Dec 23, 2020, 7:51:46 AM12/23/20
to tiddl...@googlegroups.com
Hi Gurudeep,
May be something like the small saver like what Nathan shared above.


Best wishes
Mohammad


Reply all
Reply to author
Forward
0 new messages