Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Call HTML file from a Python program
Qt 6.11 is out! See what's new in the release blog

Call HTML file from a Python program

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 3.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PythonQTMarlem
    wrote on last edited by PythonQTMarlem
    #1

    Hello,

    my text editor has a program help as an HTML file.
    I solved the call of this HTML file as follows:

        def help displays ():
            self.statusBar (). showMessage ("Show help")
            filename = os.path.abspath (".") + '\\ Help.html'
            filename = os.path.realpath (filename)
            webbrowser.open_new_tab (filename)
    

    when I call my program from PyCharm it works.
    when I start my program from the Dos window (Windows 10),
    then go to https://www.google.de/#spf=1593888337401.

    Question:
    How can I use Python to call an HTML file so that it ALWAYS works?
    it should also work on all operating systems.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Where is this help file ?
      Maybe its just a path issue.
      When run from PyCharm, it current dir is set to the correct one.
      When run from cmd prompt, its not.

      or something like that.

      You could add some checks to see if file really exists when run from prompt.

      #include <QFileInfo>
      
      bool fileExists(QString path) {
          QFileInfo check_file(path);
          // check if path exists and if yes: Is it really a file and no directory?
          return check_file.exists() && check_file.isFile();
      }
      (c++ but should be somewhat the same in python)
      
      1 Reply Last reply
      0
      • P Offline
        P Offline
        PythonQTMarlem
        wrote on last edited by
        #3

        Hi,
        the Helpfile is in the same directory like the pythonfile.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PythonQTMarlem
          wrote on last edited by
          #4

          @PythonQTMarlem said in Call HTML file from a Python program:

          os.path.abspath (".")

          I know my Problem:

          os.path.abspath (".")
          

          don't return the directory where my python-file is.

          Question:
          How I get the directory where my python-file is?

          mrjjM 1 Reply Last reply
          0
          • P PythonQTMarlem

            @PythonQTMarlem said in Call HTML file from a Python program:

            os.path.abspath (".")

            I know my Problem:

            os.path.abspath (".")
            

            don't return the directory where my python-file is.

            Question:
            How I get the directory where my python-file is?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @PythonQTMarlem

            Hi
            In c++ this gives me the folder where the .exe is
            qDebug() << "App path : " << qApp->applicationDirPath();

            However, i dont know if it will work the same with python ?
            the py file sort of being the exe but not really.

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              @PythonQTMarlem said in Call HTML file from a Python program:

              How I get the directory where my python-file is?

              os.path.abspath(__file__)
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • P Offline
                P Offline
                PythonQTMarlem
                wrote on last edited by
                #7

                @mrjj
                filename = QApplication.applicationDirPath() doesn't work.
                it returns this directory: C:/ProgramData/Anaconda3

                @SGaist
                it returns C:\Projekte\Python\PQT-Texteditor\PQTTexteditor.py

                Question:
                how can i get this without "PQTTexteditor.py"

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  By looking at the os.path documentation: dirname.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • P Offline
                    P Offline
                    PythonQTMarlem
                    wrote on last edited by
                    #9

                    this is the solution:

                            def hilfeanzeigen():
                                self.statusBar().showMessage("Hilfe anzeigen")
                                filename = os.path.dirname(os.path.abspath(__file__)) + '\\Hilfe.html'
                                webbrowser.open_new_tab(filename)
                    
                    
                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      PythonQTMarlem
                      wrote on last edited by
                      #10

                      it's not perfect! it doesn't work in Ubntu.

                      Question:
                      What I have to change?

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        PythonQTMarlem
                        wrote on last edited by PythonQTMarlem
                        #11

                        I try this:

                                def hilfeanzeigen():
                                    self.statusBar().showMessage("Hilfe anzeigen")
                                    filename = os.path.dirname(os.path.abspath(__file__)) + '//Hilfe.html'
                                    webbrowser.open_new_tab(os.path.abspath(filename))
                        

                        it works in Windows and in Ubuntu.
                        But I don't know if it is the profi solution.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          If you want to be safe use os.path.join to build paths.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved