0

I try to launch python script form HTML button. My python script work on text files and reorganize them in folders, so it doesn't have return, it's only an action.

This is my python script

import os

# Compress tabfile

folder_edition  = 'D:\\folder_edition'
folder_destination = 'D:\\folder_destination'

for the_file in os.listdir(folder_edition):

    if the_file.endswith(".tab"):

        fichier_source = folder_edition + "\\" + the_file
        fichier_temporaire = folder_compress + "\\" + the_file + "_temp.tab"
        fichier_compress = folder_compress + "\\" + the_file.replace("edition_","cclo_")

        print fichier_source

        result = file(fichier_source,"r").read().replace("  ", " ").replace("     ", " ").replace("    ", " ").replace("   ", " ").replace("  ", " ").strip()
        file(folder_compress + "\\" + the_file + "_temporaire.tab","w").write(result)

        fichier_in = open(fichier_temporaire,"r")
        fichier_out = open(fichier_compress, "w")

        for ligne in fichier_in:
            if not (ligne.startswith("#") or ligne.startswith(" #") or ligne.startswith("  #") or ligne.startswith("   #") or ligne.startswith("    #") or ligne.startswith("     #")):
                fichier_out.write(ligne)

        fichier_in.close()
        fichier_out.close()

        f = file(fichier_compress,"r")
        chaine = f.read()
        f.close() 
        result=chaine.replace("\n", " ").replace("  ", " ")
        f = file(fichier_compress,"w")
        f.write(result)
        f.close()

        os.remove(fichier_temporaire)

And I tried to call it with a button in HTML

<html>
<div id = "global">
    <button class="button_tab" onclick="admin_tab('compress')">Compress</button>
</div>
</html>

And my script where I call my script with Ajax and JQuery

function admin_tab(var_operation)
{
    if (var_operation = 'compress') {
        $.ajax({
            type:'GET',
            url: 'script\\python\\compress.py',
            timeout: 3000,
            async:true,
            success: function(response){
                alert('Yes !');
            },
            error: function() {
              alert('No !'); }
        });
    }
};

When I click on the button, HTML correctly read my Python script, but I've any action in folders ...

What can I do for have a similar action, like double click ?

Geo-x
  • 275
  • 1
  • 5
  • 22
  • It's not clear why you thought this would work. It can *access* the file, sure, but *execute* it? – jonrsharpe Jul 07 '16 at 08:13
  • So how can I execute this script ? – Geo-x Jul 07 '16 at 08:14
  • See e.g. http://stackoverflow.com/q/11747527/3001761 – jonrsharpe Jul 07 '16 at 08:16
  • I don't understand the answers of the other post – Geo-x Jul 07 '16 at 08:36
  • You will need to be more specific than *"don't understand"*. Spend some time on research, then if you still are unclear [edit] this question to describe precisely what you still need to know. See [ask]. – jonrsharpe Jul 07 '16 at 08:40
  • You need to run the script on the server, take a look at Flask. You can wrap the script with a route decorator – Sinan Guclu Jul 07 '16 at 08:40
  • It's just not the same case, I don't need connection to database, I just want to wrtie and reorder folders – Geo-x Jul 07 '16 at 08:42
  • Thank you Sinan Guclu, I tried to resolve my problem with Flask (I'm not a very good developper of python, it's for that what it's difficult for me to understand the answers of the other subject) – Geo-x Jul 07 '16 at 08:44
  • Do you know can I make an answer for this question ? I don't have button `Post your answer` and the answer of the other subject don't work for me... – Geo-x Oct 10 '16 at 07:53

0 Answers0