question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run Python script from a string

See original GitHub issue

Let me first say that this package looks amazing!

I would like to run a small snippet of Python code (provided by my users) without the need of creating a new <script> tag in the page for each snippet of code I receive. Is it currently possible to use the Brython main function on a string of Python code?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
schmavecommented, Jan 29, 2020

If you want to run Python code without making an script tag for each bit of Python, you can use Brython’s python_to_js function like this:

<script type="text/javascript" src="path/to/brython.js"></script>
<script type="text/javascript">
window.eval(__BRYTHON__.python_to_js('x = 5; print(x);'));
</script>
2reactions
radiantonecommented, May 4, 2021

All you need to do is add result and return it like this. Can you please add this? Thank you!!

try{
    ;$locals.$line_info = "1,__main__";if($locals.$f_trace !== _b_.None){$B.trace_line()};_b_.None;
    var result = $B.mul(5,3);
    $B.leave_frame({$locals, value: _b_.None})
    return result;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Execute a String of Code in Python - GeeksforGeeks
Here we use the exec() function to solve the code contained inside a variable. exec() function is used for the dynamic execution of...
Read more >
How do I execute a string containing Python code in Python?
The most logical solution would be to use the built-in eval() function .Another solution is to write that string to a temporary python...
Read more >
Python's exec(): Execute Dynamically Generated Code
Python's built-in exec() function allows you to execute any piece of Python code. With this function, you can execute dynamically generated code ......
Read more >
Python Program to execute a String of Code in Python
Python Program to execute a String of Code in Python ; Input: ; Output: 9 ; Step 1- Define a function to solve...
Read more >
Python Exercise: Execute a string containing Python code
Python Exercises, Practice and Solution: Write a Python program to execute a string containing Python code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found