#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0

    Python in Wordpress.org Site


    Hello world!

    I read this forum a lot. Now, I have a question I'd like to see if anyone here can answer.

    How could I run a basic 'print 'hello world'' code on a wordpress blog?

    There is a 5-star rated plugin for wordpress that is called 'Run This!' It doesn't explain itself much, but it gives me this:

    'This plugin lets you execute code snippets you post in your blog. You simply add a class "run-this" to the tag surrounding the code and a lang attribute matching the programming language used. The code is sent over to Ideone to run; results are displayed on your blog, underneath the code.'

    How would I actually implement this with a basic code such as print 'hello world'? I suppose this might be more of an html question.

    Does anyone have any guidance they can give me?
    Last edited by adampjr; June 17th, 2013 at 03:39 PM. Reason: epic spelling fail
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    The code is sent over to Ideone to run; results are displayed on your blog, underneath the code.
    That sounds massively inefficient and slow. What is your actual end goal? (I assume it isn't actually printing "hello world")
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by E-Oreo
    That sounds massively inefficient and slow. What is your actual end goal? (I assume it isn't actually printing "hello world")
    It's just to print hello world, really.

    Just kidding.

    I want to display sets of randomized numbers and strings (using 'choice' as imported from 'random') I don't have much big specific, I just more want to know how to do it.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    I'll help you out here, but just so you know, this is a bad idea and you shouldn't do it. There is a reason why there are no plugins for WordPress that let you run Python code - it's because doing so is extremely inefficient. For WordPress to run a Snippet of Python code, it has to create a new process and boot up the Python interpreter. There's a huge amount of overhead in doing that (on the order of tenths of a second) compared to running PHP code to do it (no extra overhead). Randomizing a set of numbers and strings is a very simple operation, probably on the order of single or double digit milliseconds - so the amount of overhead you're incurring to do that in Python is massive.

    The 'Run This!' plugin might have a 5-star rating, but it only has one vote so that's pretty much meaningless. Sending the Python code to a remote API to run it incurs an additional overhead on the order of whole seconds. Plus you run the risk of the remote API going down, which would probably add tens of seconds to the load time of your page.

    You can add a short tag in a plugin that will let you execute an arbitrary script:
    PHP Code:
    <?php
    /*
    Plugin Name: Don't use me
    */

    add_shortcode('runpy', function($atts) {
      return 
    shell_exec("python {$atts['script']}");
    });
    (untested)

    Then in your WordPress post you would do something like:
    Code:
    [runpy script="/path/to/python_code.py"]
    However, note that this allows someone to run arbitrary code on your server. Anyone with access to use the short code will have complete and unrestricted access to your server.

    A much better idea is to just do it in PHP to begin with:
    PHP Code:
    <?php
    /*
    Plugin Name: Display random stuff
    */

    add_shortcode('runpy', function($atts) {
      
    $set = array(
        
    'value 1',
        
    4345,
        
    'value 3',
        
    59348
      
    );
      return 
    $set[rand(0count($set)-1)];
    });
    That way you avoid both the performance and the security concerns.
    Last edited by E-Oreo; June 17th, 2013 at 11:40 PM.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0
    Hey thanks. I'll probably do it tomorrow to try and figure it out just because, but because of what you said at the beginning I'll probably drop it after that (depending on how it performs).

    Thanks either way.

    ETA: I'm sure I can just as easily learn the same things in more compatible codes, I just knew a bit of python so that's why I was thinking along these lines.

IMN logo majestic logo threadwatch logo seochat tools logo