cleanup a bit
Bobby Grayson 1 day ago 2 files (+8, -68)
MODIFIED
lib/blog/python_runner.ex
MODIFIED
lib/blog/python_runner.ex
@@ -1,17 +1,8 @@defmodule Blog.PythonRunner do- @moduledoc """- Module for interacting with Python code using Pythonx.- """require Logger- @doc """- Initializes the Python interpreter with minimal configuration.- This only needs to be called once per application start.- """def init_python dotry do- # Check if Python is already initialized- # This is idempotent - safely handles being called multiple timesconfig_str = """[project]name = "python_demo"@@ -19,21 +10,19 @@ version = "0.0.1"requires-python = ">=3.8""""- # Log the environment variables to help with debuggingpython_path = System.get_env("PYTHONX_PYTHON_PATH", "not set")cache_dir = System.get_env("PYTHONX_CACHE_DIR", "not set")-- # Use uv_init without options - relying on environment variablesPythonx.uv_init(config_str):okrescuee in RuntimeError ->- if String.contains?(Exception.message(e), "already been initialized") do- Logger.info("Python interpreter was already initialized, continuing")- :ok- else- Logger.error("Failed to initialize Python: #{Exception.message(e)}")- {:error, Exception.message(e)}+ case String.contains?(Exception.message(e), "already been initialized") do+ true ->+ Logger.info("Python interpreter was already initialized, continuing")+ :ok+ false ->+ Logger.error("Failed to initialize Python: #{Exception.message(e)}")+ {:error, Exception.message(e)}ende ->@@ -42,49 +31,6 @@ {:error, inspect(e)}endend- @doc """- Runs a simple Python "Hello World" program and logs the result.-- ## Examples-- iex> Blog.PythonRunner.run_hello_world()- {:ok, "Hello from Python 7!"}- """- def run_hello_world do- case init_python() do- :ok ->- try do- # Use eval/2 which takes Python code and a map of variables- {result, _} = Pythonx.eval("f'Hello from Python {3 + 4}!'", %{})- message = Pythonx.decode(result)-- # Log the result- Logger.info("Python execution result: #{message}")-- {:ok, message}- rescue- e ->- Logger.error("Error executing Python code: #{inspect(e)}")- {:error, "Failed to execute Python code: #{inspect(e)}"}- end-- {:error, reason} ->- {:error, "Python initialization failed: #{reason}"}- end- end-- @doc """- Runs a custom Python script with the provided code.-- ## Parameters-- * `code` - The Python code to execute as a string-- ## Examples-- iex> Blog.PythonRunner.run_python_code("print('Custom message')")- {:ok, "Custom message\\n"}- """def run_python_code(code) when is_binary(code) docase init_python() do:ok ->
@@ -6,13 +6,7 @@ @impl truedef mount(_params, _session, socket) do{:ok, assign(socket,result: nil,- code: """-def hello_world():- return "Hello from Python! ๐"--result = hello_world()-result-""",+ code: "",executing: false,error: nil)}