101 lines
3.3 kB
1
defmodule BlogWeb.PythonDemoLive do
2
use BlogWeb, :live_view
3
require Logger
4
5
@impl true
6
def mount(_params, _session, socket) do
7
{:ok, assign(socket,
8
python_result: nil,
9
custom_code: "print('Hello from custom Python code!')",
10
custom_result: nil,
11
error: nil,
12
meta_attrs: [
13
%{name: "title", content: "A python interpreter in the browser, run via Elixir webserver"},
14
%{
15
name: "description",
16
content:
17
"Just go nuts. You really can run arbitrary python code right now"
18
},
19
%{
20
property: "og:title",
21
content: "lol a poor man's IDLE or something?"
22
},
23
]
24
)}
25
end
26
27
@impl true
28
def handle_event("run-hello-world", _params, socket) do
29
# Run the hello world function
30
case Blog.PythonRunner.run_hello_world() do
31
{:ok, result} ->
32
{:noreply, assign(socket, python_result: result, error: nil)}
33
34
{:error, error_msg} ->
35
{:noreply, assign(socket, error: error_msg)}
36
end
37
end
38
39
@impl true
40
def handle_event("run-custom-code", %{"code" => code}, socket) do
41
case Blog.PythonRunner.run_python_code(code) do
42
{:ok, result} ->
43
{:noreply, assign(socket, custom_result: result, error: nil)}
44
45
{:error, error_msg} ->
46
{:noreply, assign(socket, error: error_msg)}
47
end
48
end
49
50
@impl true
51
def render(assigns) do
52
~H"""
53
<div class="mx-auto max-w-3xl p-4">
54
<h1 class="text-2xl font-bold mb-4">Python Interoperability Demo</h1>
55
56
<div class="mb-8 p-4 bg-gray-100 rounded-lg">
57
<h2 class="text-xl font-semibold mb-2">Hello World Example</h2>
58
<p class="mb-4">Run a simple Python "Hello World" program:</p>
59
60
<button phx-click="run-hello-world" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
61
Run Hello World
62
</button>
63
64
<%= if @python_result do %>
65
<div class="mt-4 p-3 bg-white rounded border border-gray-300">
66
<h3 class="font-semibold text-gray-700">Result:</h3>
67
<pre class="mt-2 p-2 bg-gray-50 rounded"><%= @python_result %></pre>
68
</div>
69
<% end %>
70
</div>
71
72
<div class="p-4 bg-gray-100 rounded-lg">
73
<h2 class="text-xl font-semibold mb-2">Custom Python Code</h2>
74
<p class="mb-4">Enter your own Python code to execute:</p>
75
76
<form phx-submit="run-custom-code">
77
<textarea name="code" rows="5" class="w-full p-2 border border-gray-300 rounded mb-4 font-mono"><%= @custom_code %></textarea>
78
79
<button type="submit" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded">
80
Execute Code
81
</button>
82
</form>
83
84
<%= if @custom_result do %>
85
<div class="mt-4 p-3 bg-white rounded border border-gray-300">
86
<h3 class="font-semibold text-gray-700">Result:</h3>
87
<pre class="mt-2 p-2 bg-gray-50 rounded"><%= @custom_result %></pre>
88
</div>
89
<% end %>
90
91
<%= if @error do %>
92
<div class="mt-4 p-3 bg-red-100 rounded border border-red-300 text-red-700">
93
<h3 class="font-semibold">Error:</h3>
94
<pre class="mt-2 p-2 bg-red-50 rounded"><%= @error %></pre>
95
</div>
96
<% end %>
97
</div>
98
</div>
99
"""
100
end
101
end
102