GithubHelp home page GithubHelp logo

tabreturn / thonny-py5mode Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 8.0 1.66 MB

A py5 plug-in for Thonny

License: Do What The F*ck You Want To Public License

Python 100.00%
py5 thonny processing-python plug-in

thonny-py5mode's Introduction

thonny-py5mode

A py5 plug-in for Thonny

Use the Thonny Python IDE as a Processing PDE alternative for creative coding. Thonny-py5mode is a plug-in that installs and configures Thonny for use with py5, a Python (3.8+) framework that leverages Processing's core libraries.

Instructions

If you already have some version of Thonny that includes Python 3.8+ on your computer, you can skip straight to step 4.

  1. Download and install the Thonny IDE from https://github.com/thonny/thonny/releases. Expanding the Assets will reveal the downloads for Windows/macOS/Linux --

    For your convenience, here are direct links to the downloads for Thonny 4.1.4:

    The thonny-py5mode plug-in will run fine with Thonny 4 because it ships with Python 3.10.

  2. Start Thonny. If you're running it for the first time, just accept the Standard settings.

  3. Once Thonny is open, select Tools > Manage plugins...

    Then search for and install the thonny-py5mode plug-in --

    You must restart Thonny after this step.

  4. When you've restarted Thonny, select py5 > Imported mode for py5 --

    Click Proceed to download, extract, and set up JDK-17 (if you need to know: the plug-in installs JDK in the Thonny user-config directory). Thonny only needs to download JDK the first time you switch to imported mode.

    You'll be notified once this process completes --

  5. This step is optional. There are several Thonny settings that I recommend you apply for working with py5 (including a Processing 4 inspired theme, Kyanite). You can apply those settings in one simple step using py5 > Apply recommended py5 settings

  6. When the py5 Imported mode for py5 option is checked, Thonny can run your py5 code --

    Test out an imported mode sketch by clicking the green play button (or using the F5 or Ctrl+R keyboard shortcuts). Here is some code:

    def setup():
        size(300, 200)
        rect_mode(CENTER)
    
    def draw():
        rect(mouse_x, mouse_y, 10, 10)

    NOTE: This mode also runs static mode sketches (when you don't need a draw() function for animation).

    Click the stop-sign (๐Ÿ›‘) button in the Thonny toolbar to stop your sketch.

Module Mode Sketches

To run a py5 module mode sketch, deactivate/uncheck py5 > Imported mode for py5 first so that Thonny returns to its normal run behaviour (for running any Python script). As an example, you can try this code:

import py5

def setup():
    py5.size(300, 200)
    py5.rect_mode(py5.CENTER)

def draw():
    py5.square(py5.mouse_x, py5.mouse_y, 10)

py5.run_sketch()

Note that module mode requires an import py5 and run_sketch() line, and py5. prefixes.

Useful py5 resources

py5 is a new version of Processing for Python 3.8+. It makes the Java Processing jars available to the CPython interpreter using JPype. It can do just about everything Processing can do, except with Python instead of Java code. Here are some useful py5 resources (alphabetically listed) --

Credits

Thanks villares for inspiring me to develop this plug-in, hx2A for the awesome py5 project, and the Thonny folks for their fantastic IDE. The Color selector incorporates Juliette Monsel's excellent tkColorPicker module.

Todo List

See discussions on GitHub repo. This plug-in is a work in progress ... please report issues here.

thonny-py5mode's People

Contributors

aivarannamaa avatar hx2a avatar tabreturn avatar villares avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

thonny-py5mode's Issues

Feature idea: Menu command to 'open enclosing folder'

I miss this from the Processing IDE, I had a proof of concept working, but I have since broken my Thonny env and I have yet to find my way again on how to play with its plug-ins...

    def open_folder(self) -> None:
        """Open the enclosing folder to the current file."""
        import subprocess
        import platform
        self.editor = self.workbench.get_editor_notebook().get_current_editor()
        try:
            filename = self.editor.get_filename()
        except AttributeError:
            print("Do you have an open file?")  # maybe a message?
            return
        else:
            path = os.path.dirname(filename)
            print(sys.platform)
        if sys.platform == 'darwin':
            subprocess.Popen(['open', path])
        elif sys.platform == 'linux':
            subprocess.Popen(["xdg-open", path])
        else: # 'win32'
            # os.startfile(path) # I should test this someday
            subprocess.Popen(["explorer", path])

    def load_plugin(self) -> None:
        ...  # other menus
        self.workbench.add_command(
            command_id='open_folder',
            menu_name='tools',
            command_label="Open enclosing folder",
            handler=self.open_folder,
            default_sequence='<Control-Alt-k>',
            extra_sequences=['<<CtrlAltKInText>>'],
        )

Keep colorchooser window open indefintely

I'm opening a TKinter colorchooser window with this code:

colors = colorchooser.askcolor(title='Color Selector')

I want to copy color values from the colorchooser and paste them into my other Tkinter window (the code editor). I want to leave the colorchooser open, and click the code editor window to focus on it. But -- while the colorchooser is open, I cannot focus on anything else until I close it.

Plugin fails to load on Raspberry Pi

Installed Thonny on both NixOS and on Raspberry Pi OS.
After installing thonny-py5mode and restarting Thonny, I get the following error

Warning: Failed loading plugin thonnycontrib.backend.py5_imported_mode_backend.
See backend.log for more info.

The p5 mode option does not appear in the menu bar.

pyde extension support

I realized that Thonny doesn't recognize the syntax when opening a file with .pyde extension. It would be awesome if the extension supports opening pyde files since this is the default extension used be the Processing IDE when saving python sketches.

Forget the sketch window position after restarting

"yesterday in class (Windows OS) I had 2 students who hat their sketch windows placed outside the visible screen for some reason. I managed to bring it back but it was very hard. I wonder how could we avoid this. Should perhaps Thonny forget the sketch window position after restarting?"

Maybe a py5 installation issue...

As I was creating a VM for a student based on a Xubuntu OVA I had, installed via plugin, JDK download successful, but in the end, importing py5 failed.

I was able to work around this issue by uninstalling py5 and re-installing it from the Thonny package management shell with pip3 install py5[jupyter]. The reason for the [jupyter] is a hint at the error message.

So maybe this is not a thonny-py5mode plugin issue, but a py5 installation issue, I don't know.

CC @py5coding, please see if this is related to the Jupyter packages support separation.

import py5 in the REPL resulted in this:

Python 3.10.6 (/home/xubuntubox/apps/thonny/bin/python3.10)
>>> import py5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/py5/__init__.py", line 85, in <module>
    from py5_tools.magics import load_ipython_extension  # noqa
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/py5_tools/magics/__init__.py", line 22, in <module>
    from .drawing import DrawingMagics, DXFDrawingMagic
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/py5_tools/magics/drawing.py", line 27, in <module>
    from IPython.display import display, SVG, Image
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/IPython/__init__.py", line 52, in <module>
    from .core.application import Application
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/IPython/core/application.py", line 27, in <module>
    from IPython.core import release, crashhandler
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/IPython/core/crashhandler.py", line 28, in <module>
    from IPython.core import ultratb
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/IPython/core/ultratb.py", line 101, in <module>
    import stack_data
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/stack_data/__init__.py", line 1, in <module>
    from .core import Source, FrameInfo, markers_from_ranges, Options, LINE_GAP, Line, Variable, RangeInLine, \
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/stack_data/core.py", line 19, in <module>
    from stack_data.utils import (
  File "/home/xubuntubox/.config/Thonny/plugins/lib/python3.10/site-packages/stack_data/utils.py", line 11, in <module>
    from asttokens import ASTText
ImportError: cannot import name 'ASTText' from 'asttokens' (/home/xubuntubox/apps/thonny/lib/python3.10/site-packages/asttokens/__init__.py)
>>> 

Add display window positioning parameters to run command

The py5 run_sketch now accommodates positioning (and other) args. For example --

run_sketch code.py --py5_options external location=0,0 --sketch_args 1 2 3

The next step is to record and recall those values so that the display window opens in the same position it was last closed (see issue #8).

Investigate crash issue

YGqdIAd0 png medium

Traceback (most recent call last):
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\tkinter_init_.py", line 1892, in call
return self.func(*args)
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://workbench.py", line 1068, in dispatch_from_menu
dispatch(None)
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://workbench.py", line 994, in dispatch
handler()
File "C:\Users\alexandre.villares\AppData\Roaming\Python\Python38\site-packages\thonnycontrib\thonny-py5mode_init_.py", line 151, in execute_module_mode
running.get_shell().submit_magic_command(cd_cmd_line + exe_cmd_line)
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://shell.py", line 224, in submit_magic_command
self.text.submit_command(cmd_line, ("magic",))
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://shell.py", line 408, in submit_command
self._try_submit_input()
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://shell.py", line 1089, in _try_submit_input
self._submit_input(submittable_text)
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://shell.py", line 1231, in _submit_input
get_runner().send_program_input(text_to_be_submitted)
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://running.py", line 313, in send_program_input
self._proxy.send_program_input(data)
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://running.py", line 972, in send_program_input
self._send_msg(InputSubmission(data))
File "C:\Users\alexandre.villares\AppData\Local\Programs\Thonny\lib\site-packages\thonny\http://running.py", line 965, in _send_msg
self._proc.stdin.write(serialize_message(msg) + "\n")
OSError: [Errno 22] Invalid argument

Use py5/jpype to check/set JAVA_HOME

"py5 can check if JAVA_HOME exists, and if not, see what jpype.getDefaultJVMPath() returns? If that is not version >= 17, jpype will not be able to start py5. Instead of failing, check in the location where the Python JDK library puts the installs. If Java 17 is found there, py5 can direct jpype to that location. If it isn't found, display a suitable error message."

Call jpype.getDefaultJVMPath() so it isn't necessary to set JAVA_HOME?

Add a note about Windows

Cheers Tristan!

Maybe you might want to consider adding a note that at this point this installation process is not working on Windows :(

The reason is noise cannot be installed with pip on Windows.

I'm trying to do it with conda/miniconda and I'll report, but it adds a nasty layer of complexity.

Tentative instructions:

  • Install miniconda https://docs.conda.io/en/latest/miniconda.html
  • Open the "Command Prompt" terminal and use conda env create -n py5coding -f http://py5.ixora.io/install/py5_environment.yml
  • Change under Thonny Tools > Options...
    • Under interpreter tab, change to Alternative interpreter, find the conda py5coding env.
      (on Linux the env appears on Thonny, but it didn't on Windows for me, I found out and filled manually)
    • I COULDN'T MAKE IT WORK :(
    • the plugin site-packages is different from the interpreter site-packages and I couldn't install the plug-in

Warning message about backend until JDK installed

py5coding/py5generator#88

I have installed Thonny 4.0-beta3-py38portable on Windows and installed thonny-py5mode (that means py5, jdk-17) YAY!
Note: There was a red warning message in the console about the plugin being unable to load, and I had to restart Thonny again after the plugin downloaded the JDK, then it disappeared.

Store display window location values in Thonny config?

This command will open the display window at location 100, 100 --

run_sketch code.py --py5_options external location=100,100

The external parameter will instruct the sketch to report the window locations to stderr. Those values should determine the display window location for the next run.

The window location values should probably reset after each Thonny session (so that a new Thonny session runs the sketch in the default location / center again).

Develop a better color-mixer

The Tkinter built-in color-mixer differs between operating systems. It currently steals focus from the editor window (so you cannot leave it open alongside the editor and copy/paste color values between the mixer and editor). Maybe a bespoke color-mixer (with HSB, RGB, hexadecimal) that docks in Thonny interface is what's required?

Consider classpath support feature

py5coding/py5generator#110 (reply in thread)

You can set the environment variable $PY5_JARS to a directory. Any jars in that directory or a subdirectory will be automatically added.

If you are using the run_sketch utility, you can add jars with the --classpath command line argument.

If you are using Thonny, the py5 plugin can be enhanced to support either of these two options if it does not do one of them already.

No luck running on M1 Mac

Hi! I tried installing and running py5, but it isn't able to find the JDK.

There were no error messages during the installation and configuration of the plugin, but when I tried to run a script I got:

py5 is unable to start a Java 17 Virtual Machine.
This library requires Java 17 to be installed and a properly set JAVA_HOME environment variable.
Here is some debug info about your installation that might help you identify the source of this problem.
{'JAVA_HOME environment variable': '/Users/calua/Library/Thonny/jdk-17', 'jvm version': (0, 0, 0), 'default jvm path': '/Users/calua/Library/Thonny/jdk-17/Contents/Home/lib/libjli.dylib'}
Traceback (most recent call last):
  File "/Users/calua/Library/Python/3.9/lib/python/site-packages/py5_tools/tools/run_sketch.py", line 52, in <module>
    main()
  File "/Users/calua/Library/Python/3.9/lib/python/site-packages/py5_tools/tools/run_sketch.py", line 44, in main
    imported.run_code(
  File "/Users/calua/Library/Python/3.9/lib/python/site-packages/py5_tools/imported.py", line 135, in run_code
    _run_code(
  File "/Users/calua/Library/Python/3.9/lib/python/site-packages/py5_tools/imported.py", line 259, in _run_code
    _run_sketch(sketch_path, classpath, exit_if_error)
  File "/Users/calua/Library/Python/3.9/lib/python/site-packages/py5_tools/imported.py", line 196, in _run_sketch
    import py5
  File "/Users/calua/Library/Python/3.9/lib/python/site-packages/py5/__init__.py", line 72, in <module>
    raise RuntimeError("py5 is unable to start Java 17 Virtual Machine")
RuntimeError: py5 is unable to start Java 17 Virtual Machine

The options show that JAVA_HOME is set:

Screen Shot 2022-06-14 at 18 54 36

Crtl+Alt+K shortcut for opening enclosing folder still has some code editor behavior

I think the Ctrl+Alt+K is triggering some kind of "forward delete" in the code editor, I think it is the same as Ctrl+K.
It does open the folder, but it can mess up the code being edited.

I think I have investigated this before but I can't remember what I found out (in my personal proof of concept I was using Alt+K)
I'm not sure how we could fix this.

thonny-py5mode plugin installs incompatible JDK runtime

Using the Thonny plugin by calling "thonny-py5mode" I've got into an issue related to the architecture choice for the JDK-17 package. My test environment is a Raspberry Pi-3B using architecture aarch64. The JDK, which was automatically installed by the plugin, was incompatible. As a temporary fix, I've manually downloaded an OpenJDK 17 and fixed the PATH via bash and corrected the JAVA_HOME variable inside the Thonny settings. This fix works OK. Please analyze the automatic install routine of the plugin in order to download a hardware compatible JDK runtime.

Network drive support

Trying to run thonny-py5-mode on a file across a network fails. Note the \\ prefix in the file path below, which should include a drive-letter, i.e. D:\\. Perhaps this is a problem for files on external/flash drives, too?

%Run 'C:\Users\<user>\AppData\Roaming\Python\Python38\site-packages\py5_tools\tools\run_sketch.py' '\\example.domain.location\<file_path>\file.py'

A bunch of enhancements (to split into several issues/tasks)

"py5 can check if JAVA_HOME exists, and if not, see what jpype.getDefaultJVMPath() returns? If that is not version >= 17, jpype will not be able to start py5. Instead of failing, check in the location where the Python JDK library puts the installs. If Java 17 is found there, py5 can direct jpype to that location. If it isn't found, display a suitable error message."

  • Call jpype.getDefaultJVMPath() so it isn't necessary to set JAVA_HOME?
  • 'JDK-11 installed and JAVA_HOME set' --> f'JDK-{_REQUIRE_JDK} installed and JAVA_HOME set' instead.
  • py5_tools.reference.PY5_DIR_STR for syntax highlighting (instead of module.PY5_ALL_STR)?
  • Note (if/where applicable) that importing py5 will start the JVM, but importing py5_tools will not.
  • ...

Variable JAVA_HOME not detected automatically

I've succeeded to install and use the thonny-py5mode library, but only once I've modified the JAVA_HOME variable, which is written in the "Options" window. Its value is visible with an: echo $JAVA_HOME at the terminal command prompt. It would be much easier for beginners if a startup, or install script would detect and processes the existing variable.

Apply patched_editor_autocomplete upon applying imported mode

py5 auto-completion (in imported mode) only works if one starts the editor with Import mode for py5 activated (when the user last closed Thonny). This should apply as soon as Import mode for py5 activates (and, when necessary, after JDK installs).

Not being able to run a sketch

Hi,

I am getting the following error when I try to run a sketch:

`def setup():
size(300, 200)
rect_mode(CENTER)

def draw():
rect(mouse_x, mouse_y, 10, 10)`

image

Can you please tell me what the issue is?

Thanks in advance!

When py5 auto-completion is active, other imported modules won't auto-complete/list methods

Steps to repeat:

  1. Install a package, say cowsay
  2. Activate py5 > Imported mode for py5
  3. Try something like:
    import cowsay
    cowsay.  # now press ctrl+space, but there are no methods listed

But, if one deactivates py5 > Imported mode for py5, the auto-completion works fine for cowsay.

Note: this seems to work fine with standard modules, such as time. It also looks like maybe everything in plug-ins works fine? (things that installed with the plug-in, like tkcolorpicker; pymunk installed as a plug-in after Imported mode for py5 re-activated)

Note: this is a Thonny 4 issue.

Sort Python 3.10 / Thonny 4 support

For reasons I need to investigate properly, the plug-in won't run on Thonny 4 (Python 3.10).

For now, users should stick to Thonny 3.3.7 (the alt versions that include Python 3.9.5).

No JVM shared library file (jvm.dll) found

Hi,

I'm trying to use Thonny to run a code that uses pymunk, but it gives an error "No JVM shared library file (jvm.dll) found".

I went to the C:\Users\Admin\AppData\Roaming\Thonny\ folder and confirmed that the JDK is installed (the jdk-17 folder is there).
I understand then that Thonny is not accessing the jdk folder... could this be the problem? Could you suggest a solution, please?

thanks

After uninstalling py5mode on Windows, Thonny is broken

I uninstalled py5mode, and upon restarting Thonny I got the following error:

image

After closing this error, the IDE is frozen.

But then I uninstalled and re-installed Thonny, and the same error comes up - I even manually deleted the AppData/Local/Programs/Thonny folder before uninstalling and still the same error comes up, so effectively making Thonny unusable. Tested on Windows 10.

Not able run sketches on thonny py5 mode

So, whenever I am trying to code a sketch in py5 in thonny, it throws a error that theres no module found.
heres the snippet of the error: Warning: Failed loading plugin thonnycontrib.backend.py5_imported_mode_backend.
See backend.log for more info.
Traceback (most recent call last):
image

JAVA_HOME environment variable

Hi!, I am having problems with Py5 in Thonny.

py5 is unable to start a Java 17 Virtual Machine.
This library requires Java 17 to be installed and a properly set JAVA_HOME environment variable.
Here is some debug info about your installation that might help you identify the source of this problem.
{'JAVA_HOME environment variable': '/Users/-------/Library/Thonny/jdk-17', 'jvm version': (0, 0, 0), 'default jvm path': '/Users /-------/Library/Thonny/jdk-17/Contents/Home/lib/libjli.dylib'}

when I run java -version in terminal

java version "20.0.2" 2023-07-18
Java(TM) SE Runtime Environment (build 20.0.2+9-78)
Java HotSpot(TM) 64-Bit Server VM (build 20.0.2+9-78, mixed mode, sharing)

I don't understand what to do, thanks for the help

Portable support enhancement ideas

I'm looking at this situation:

  • I have downloaded Thonny 4 portable (with Python 3.10.6) and
  • Installation of py5thonny mode worked fine on Windows 10.
  • First activation of "imported mode for py5" downloaded the JDK-17 as expected, everything wonderful.

Then, when I move the portable folder arround, it breaks: The JDK is there in the right place, but the JAVA_HOME information in Thonny's configs/options got outdated.

It can be hard for beginners to understand what went wrong. If you uncheck and check the "imported mode for py5" menu a new updated JAVA_HOME line is added to the Thonny options, but the old one is not removed, and as far as I have tested this still won't work (you have to remove the outaded line, I think).

How could we improve this?

I had a proof of concept portable with everything already installed, but it counted on a laucher that would set JAVA-HOME every time, and also I had to modify thonny-py5mode so it would not add JAVA_HOME to Thonny options... I think this was far from ideal. So I'd like to enable people to build their own portables, and move them arround: that would be a good thing.

Is it too dangerous to check during plug-in loading if Thonny is portable and if so if JDK-17 is already installed and somehow act on it?

I hate to add too much magic and to override something the user could change in the options panel manually but maybe for this kind of beginner friendly tool it's reasonable. I'm really not sure at all how we should proceed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.