npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@examples-ai/python-code-container

v1.1.0

Published

Python Code Container, wrapping of pyodide

Readme

python-code-container

Python Code Container, wrapping of pyodide for running Python code in the browser.

Features

  • Run Python code in the browser without a server
  • Full Python runtime with package support
  • TypeScript support with type definitions
  • React integration with hooks
  • Virtual file system for file operations
  • Singleton pattern for efficient resource usage

Installation

npm install @examples-ai/python-code-container
# or
yarn add @examples-ai/python-code-container
# or
pnpm add @examples-ai/python-code-container

Quick Start

import { PythonContainer } from '@examples-ai/python-code-container';

// Initialize the container
const container = await PythonContainer.boot();

// Execute Python code
const result = await container.run(`
print('Hello from Python!')

import math
result = math.sqrt(16)
print(f'Square root of 16 is {result}')

result
`);

console.log(result); // Output from Python

Using with React

npm install @examples-ai/python-code-container react react-dom swr
import {
  PythonContainerProvider,
  usePythonContainer,
} from '@examples-ai/python-code-container/react';

function App() {
  return (
    <PythonContainerProvider>
      <PythonEditor />
    </PythonContainerProvider>
  );
}

function PythonEditor() {
  const { pyodide, isLoading, error } = usePythonContainer();
  const [output, setOutput] = useState('');

  const runCode = async (code: string) => {
    if (!pyodide) return;
    const result = await pyodide.run(code);
    setOutput(result);
  };

  if (isLoading) return <div>Loading Python...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return (
    <div>
      <button onClick={() => runCode('print("Hello!")')}>Run Code</button>
      <pre>{output}</pre>
    </div>
  );
}

React API

PythonContainerProvider

Provides Python container context to child components. Must wrap any components using usePythonContainer.

Props:

  • children: ReactNode - Child components

usePythonContainer()

React hook to access the Python container.

Returns:

{
  pyodide: PythonContainer | null; // Container instance
  isLoading: boolean; // Loading state
  error: Error | null; // Error state
}

API Reference

  • PythonContainer.boot(): Promise<PythonContainer> - Initialize the Python container and return a singleton instance
  • run(code: string, options?: RunOptions): Promise<string> - Execute Python code and return the captured output
  • installPackage(packageName: string): Promise<void> - Install a Python package from PyPI
  • readFile(path: string, encoding?: string): string | Uint8Array - Read a file from the virtual file system
  • writeFile(path: string, content: string | Uint8Array): void - Write a file to the virtual file system
  • readdir(path: string): string[] - List the contents of a directory
  • rm(path: string): void - Remove a file from the virtual file system
  • mkdir(path: string): void - Create a directory in the virtual file system
  • globals - Access to Python global namespace for getting and setting variables
  • PythonContainer.teardown(): void - Reset the singleton instance for complete reinitialization
interface RunOptions {
  packages?: string[]; // Packages to install before execution
  homedir?: string; // Working directory path
  enablePackageAutoInstall?: boolean; // Auto-install from imports (default: true)
  filename?: string; // Filename for the code
}

Examples

File Operations

const container = await PythonContainer.boot();

// Create directories
container.mkdir('/data');

// Write files
container.writeFile('/data/input.csv', 'name,age\nAlice,30\nBob,25');

// List files
const files = container.readdir('/data');

// Read files
const content = container.readFile('/data/input.csv', 'utf8');

// Use in Python
await container.run(`
import pandas as pd
df = pd.read_csv('/data/input.csv')
print(df)
`);

// Remove files
container.rm('/data/input.csv');

Error Handling

try {
  await container.run(`
import numpy as np
np.divide(1, 0)
  `);
} catch (error) {
  console.error('Python error:', error.message);
}

License

MIT @ Jimmy Moon [email protected]