https://github.com/liaplayground/pythonprogramming
Some examples on how to use Python in LiaScript
https://github.com/liaplayground/pythonprogramming
liascript liascript-course liascript-template oer programming python
Last synced: about 2 months ago
JSON representation
Some examples on how to use Python in LiaScript
- Host: GitHub
- URL: https://github.com/liaplayground/pythonprogramming
- Owner: LiaPlayground
- License: cc0-1.0
- Created: 2021-11-18T15:03:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-18T16:45:57.000Z (over 3 years ago)
- Last Synced: 2025-03-29T18:22:56.062Z (2 months ago)
- Topics: liascript, liascript-course, liascript-template, oer, programming, python
- Homepage:
- Size: 5.86 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://LiaScript.github.io/course/?https://github.com/LiaPlayground/PythonProgramming/blob/main/README.md)
# Python Programming
If you want to teach Python or at least run some Python snippets within your
course, we currently recommend two different packages for LiaScript.1. [Pyodide](https://github.com/LiaTemplates/Pyodide)
2. [CodeRunner](https://github.com/liascript/coderunner)What is the difference? Pyodide will load all libraries into your browser and
execute it locally, while CodeRunner will compile and execute the code on a
foreign server. The first project might cover only a reduced set of libraries,
but it scales and adds plotting functionality, in contrast to the second one.
In CodeRunner, plots can at the moment only be generated with R.## 1. Pyodide
> To use this functionality, you will have to import:
>
> ``` html
>
> ```
>
> Afterwards you can attach the macro `@Pyodide.eval` to every Python-Code
> snippet.
>
> **Note: All scripts are executed in one global environment**### Examples
``` python
import sysfor i in range(5):
print("Hello", 'World #', i)sys.version
```
@Pyodide.eval---
> Currently you can only plot one image, but this is shown within the terminal
> and also stored persitently. Thus, you can switch between different versions
> of your code and the resulting images will change too.``` python
import numpy as np
import matplotlib.pyplot as pltt = np.arange(0.0, 2.0, 0.01)
s = np.sin(2 * np.pi * t)fig, ax = plt.subplots()
ax.plot(t, s)ax.grid(True, linestyle='-.')
ax.tick_params(labelcolor='r', labelsize='medium', width=3)plt.show()
plot(fig) # <- this is required to plot the fig also on the LiaScript canvas
```
@Pyodide.eval> **NOTE:** You can resize the terminal and you can click onto the image to
> enlarge it. The additional loading of libraries is only required once, then
> they are stored locally...## 2. LiaScript Code-Runner
> This allows, to run and execute your code on a real
>
> ```` markdown
>
>
> # ...
>
> ```python
> for i in range(10):
> print "Hallo Welt", i
> ```
> @LIA.eval(`["main.py"]`, `python -m compileall .`, `python main.pyc`)
> ````
>
> Afterwards you can attach the macro `@LIA.eval` to every Python-Code
> snippet.
>
> The `@LIA.eval` macro might look a bit cryptic, but it actually consists
> of three steps.
>
> 1. A list of filenames, if you use require more than one files for your
> project, then these have to be defined in order. A project can consist of
> multiple code-snippets attached to each other
> 2. A compilation command
> 3. A execution command### Example
```python
for i in range(10):
print "Hallo Welt", i
```
@LIA.eval(`["main.py"]`, `python -m compileall .`, `python main.pyc`)## Best Practice
If you you have a couple of code-snippets and you do not want to add always the
same complex macro, which might eventually change in the future. The best way is
to define a custom macro, which defines the macro with all parameters. You can
modify this in the future, just by changing the macro-definition an also the
import. It is thus not required to change every macro separately:```` markdown
# ...
```python
for i in range(10):
print "Hallo Welt", i
```
@eval```python
for i in range(10):
print "Hello World", i
```
@eval
````---
```python
for i in range(10):
print "Hallo Welt", i
```
@eval```python
for i in range(10):
print "Hello World", i
```
@eval