How can I run Python script & displa...

John
2011-06-21
2012-11-14
1 2 > >> (Page 1 of 2)
  • John

    John - 2011-06-21

    I have the Python Script plugin, but I haven't figured out if it's possible to run my currently open script so that the output displays in the built-in Python console within NPP, instead of running externally and flashing a DOS window. Is this possible?

    Thanks.

     
  • John

    John - 2011-06-21

    Thank you! I tried just that last line (based on another post) and it placed the output inside the document, which confused me. :)

     
  • John

    John - 2011-06-21

    Also, is it possible to insert a line into that script that saves the current document before it runs it?

     
  • Dave Brotherstone

    I tried just that last line (based on another post) and it placed the output inside the document, which confused me. :)

    Yes, it's normally used for working with a text document in a script, so being able to "print" directly to the document can be quite handy, but doesn't make any sense at all when you use it to run a "normal" python script.

    Also, is it possible to insert a line into that script that saves the current document before it runs it?

    Of course.

    notepad.save()
    

    Note that editor.getText() gets the current text in the buffer, whether it's saved or not.  The help file that comes with the plugin shows all the available methods of the notepad and editor objects.  The same information is on the documentation site - http://npppythonscript.sourceforge.net/docs/latest/

    Hope that helps,
    Dave.

     
  • John

    John - 2011-06-21

    Thanks again! I didn't realize it just ran whatever was there without the need to save first! Now to just figure out how to create a permanent shortcut for my run script. It seems all I can do is create a shortcut for "last run script", but then that gets reset every time I restart NPP.

     
  • John

    John - 2011-06-21

    Figured it out, thanks to the docs you linked! Thanks again for all the help!

     
  • John

    John - 2011-06-21

    Hmm, okay, one more thing I can't seem to figure out. I'd like the console to show up on NPP startup, so I tried to edit the Python Script startup.py file, but it doesn't seem to be able to save any changes. I wanted to add "console.show()" and to change the line "sys.stdout = editor" to "sys.stdout = console" (and then remove this line from my user-made script, since it wouldn't be necessary I presume), but it said to check if the file is already open elsewhere and wouldn't save.

    Is there some other way to make the console automatically open?

     
  • Dave Brotherstone

    You're actually doing exactly the right thing - you just need to open Notepad++ with administrator rights (right click the N++ icon, and click "Run as Administrator"), then you can save your changes to the startup.py file.

    As an alternative, you can create a script called startup.py in your user scripts area (just simply create it with the "New Script" option), and that gets run after the "main" startup.py script.

    Dave.

     
  • John

    John - 2011-06-21

    Wow, a thing of beauty. Of course, that didn't work until I changed LAZY to ATSTARTUP, but luckily I was able to figure that out myself!

    Also, I just realized you are the author of the plugin (duh!). Thanks for the help. I didn't realize what I had with this plugin until I read your responses. Awesome plugin.

     
  • John

    John - 2011-06-21

    Ok, one final question (seriously)! Is it possible to use Python Script to define a new entry for my Python style configuration? It seems a limitation of NPP that I can't set up a different syntax color for the Python built-ins (e.g., int, list, dict, etc.). I could add them all to the user-defined section for the KEYWORDS entry, but that would make them all the same color as the Python keywords.

    Can I somehow create a new entry called BUILT-INS that lets me define these words and set a different syntax highlighting color for them?

    Thanks again.

     
  • Dave Brotherstone

    Asking questions is what we're here for! :)

    Unfortunately not.  That's the job of the lexer (a small part of the Scintilla control that works out which bits of the text are comments, keywords, strings etc).  And if the lexer doesn't allow for parsing different lists of keywords, then it just doesn't.  It would mean changing the C++ code of the lexer itself to support it.

    The styles the lexer defines are:
    SCE_P_DEFAULT=0
    SCE_P_COMMENTLINE=1
    SCE_P_NUMBER=2
    SCE_P_STRING=3
    SCE_P_CHARACTER=4
    SCE_P_WORD=5
    SCE_P_TRIPLE=6
    SCE_P_TRIPLEDOUBLE=7
    SCE_P_CLASSNAME=8
    SCE_P_DEFNAME=9
    SCE_P_OPERATOR=10
    SCE_P_IDENTIFIER=11
    SCE_P_COMMENTBLOCK=12
    SCE_P_STRINGEOL=13
    SCE_P_WORD2=14
    SCE_P_DECORATOR=15

    That was taken from the current Scintilla.iface (http://scintilla.hg.sourceforge.net/hgweb/scintilla/scintilla/file/e3b4a49709cc/include/Scintilla.iface)

    If the N++ styles don't match up to those, then you can add these style numbers in to your langs.xml file (needs to be edited outside of N++ as N++ overwrites this file on exit).

    Cheers,
    Dave.

     
  • John

    John - 2011-06-22

    Hmm, my list of definable styles stops at COMMENTBLOCK, so perhaps I can enable WORD2 as my list of built-ins. But I don't see a lang.xml file in the NPP folders. Also, in the style configurator window, it refers to "styles.xml" but I don't see that file either. Even searching for those names on my system produced no results. Which is the right file and where can I find it?

     
  • John

    John - 2011-06-27

    Also, is it possible to use input (or raw_input) when running a file in NPP? The output in my Python console always gives an EOFError.

     
  • Dave Brotherstone

    stylers.xml is normally under %APPDATA%\Notepad++\stylers.xml

    You need to edit this file using a different editor, as N++ overwrites the file again when it closes.

    And sadly no, it's not possible to use input for a Python Console app.  That may be in the next version, but I don't know how long that will be sorry.

    Dave.

     
  • John

    John - 2011-06-27

    Thanks again! And as for the stylers.xml file, my Python section looks like this:

    <LexerType name="python" desc="Python" ext="">
                <WordsStyle name="DEFAULT" styleID="0" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="COMMENTLINE" styleID="1" fgColor="008000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="NUMBER" styleID="2" fgColor="400000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="STRING" styleID="3" fgColor="808080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="CHARACTER" styleID="4" fgColor="808080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="KEYWORDS" styleID="5" fgColor="0000FF" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" keywordClass="instre1" />
                <WordsStyle name="TRIPLE" styleID="6" fgColor="FF8040" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="TRIPLEDOUBLE" styleID="7" fgColor="408080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="CLASSNAME" styleID="8" fgColor="800000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="DEFNAME" styleID="9" fgColor="800080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="OPERATOR" styleID="10" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="IDENTIFIER" styleID="11" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
                <WordsStyle name="COMMENTBLOCK" styleID="12" fgColor="008000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
            </LexerType>

    It almost looks as if I can add anything I want, as long as I follow that xml structure. At any rate, the final three you mentioned above (SCE_P_STRINGEOL=13 SCE_P_WORD2=14 SCE_P_DECORATOR=15) aren't even listed, so it looks like it's more user-definable than I thought. I'm going to try anyway, and hope nothing explodes.  :)

     
  • John

    John - 2011-06-28

    Hmm, okay, I've figured out how to add the additional style types you mentioned, and I've got them all working except "WORD2". I edited the "langs.xml" file to include the words I wanted highlighted, and they appear in the Style Configurator, but when I type them they don't change to the set color, they remain black.

    Is there something else I need to do? Here's what I've done:

    1. Added:

    <WordsStyle name="BUILT-INS" styleID="14" fgColor="8080C0" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" keywordClass="instre2" />
    

    to the stylers.xml file. I assume keywordClass="instre2" is correct, based on other defined languages. I changed "WORD2" to "BUILT-INS" because I assume this is merely a superficial change, just as "WORD" was changed to "KEYWORDS" for the sake of clarity.

    2. In the langs.xml file I added:

    <Keywords name="instre2">Ellipsis False None NotImplemented True __import__ abs all any ascii bin bool bytearray bytes callable chr classmethod compile complex delattr dict dir divmod enumerate eval exec filter float format frozenset getattr globals hasattr hash help hex id input int isinstance issubclass iter len list locals map max memoryview min next object oct open ord pow print property range repr reversed round set setattr slice sorted staticmethod str sum super tuple type vars zip</Keywords>
    

    I have a color set and the file is set for Python (and other coloring works), but not this new list. I'm not sure what else I need to do to make it work.

     
  • John

    John - 2011-06-29

    I tried the Scintilla forums but they said Scintilla doesn't read these files directly, so it's an NPP issue……

     
  • John

    John - 2011-07-01

    Does anyone know how to make NPP read these files so I can have my built-ins highlighted? I'm so close to getting it just right!

    Thanks.

     
  • Mike Cowperthwaite

    (Just to be clear, you're no longer in the Scripting realm; this is more typical Notepad++ tech support.)
    I don't know if you've tweaked the original keyword list, but you have some redundancies with it in your posted list.
    That said, even tweaking things myself, I can't get this to work either. The extra keywords need to be passed to Scintilla's lexer; possibly Notepad++ isn't doing that. Interesting, tho, that adding the "keywordClass" attribute enables the word list display in Settings | Style Configurator, and all the words in the list are displayed. That's probably generic code in the Configurator, tho, not Python-specific.

    I also noted that the original keyword list has 'triple' which, as far as I know, is not a Python keyword. Probably was supposed to be 'tuple'.

     
  • John

    John - 2011-07-02

    Yeah, I removed the redundancies after I posted that, and I also noticed "triple" as well. I have no idea what that was, so I removed it.

    Too bad though about not getting this to work. I'll try posting elsewhere since it's more of an NPP thing. Thanks!

     
  • Mike Cowperthwaite

    Posting elsewhere probably won't help. Building your own Notepad++ and tracking down where the lexer is set up would be the most likely path to getting that to work.

     
  • John

    John - 2011-07-02

    What do you mean?

     
  • Mike Cowperthwaite

    What do you mean?

    Well, there were two statements in my post. By the first, I meant, bluntly: There aren't very many people answering questions in these forums who seem to be very astute. (davegb3 is a notable exception.) Don Ho, the author/maintainer of Notepad++, does seem to read (at least some) forum posts, but his decisions for changes and improvements are opaque and, IMNSHO, occasionally misguided. I don't think he's a Python user, so changes to the Python support will be slow in coming.

    Some months ago I posted a correction to the Python stylers.xml code (to fix STRINGEOL and add DECORATOR). If Don noticed it, he didn't bother to integrate it.

    By my second statement I meant: the source for Notepad++ is readily available. Download it, build it, fix it to work the way you want, and sign on to maintaining that version while integrating new Notepad++ changes (until such point as Don's version fixes the same thing). You can make your patch available, maybe it'll be accepted.

    Personally, for me, that's too much work. Notepad++ is very useful and good enough that I haven't bothered to look for another editor. I just live with suboptimal lexing, among the dozen or so other bugs that annoy me but that I don't expect Don to ever get around to fixing.

     
  • John

    John - 2011-07-02

    So the only way to fix the issue is to actually change NPP? There aren't any options to change, or maybe something I can do with the Python Script plugin to force NPP to update the lexer?

     
1 2 > >> (Page 1 of 2)

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks