Skip to content

Added copy button to the code cells in the Keras.io website #2247

Open
buildwithsuhana wants to merge 9 commits intokeras-team:masterfrom
buildwithsuhana:copyButton
Open

Added copy button to the code cells in the Keras.io website #2247
buildwithsuhana wants to merge 9 commits intokeras-team:masterfrom
buildwithsuhana:copyButton

Conversation

@buildwithsuhana
Copy link
Contributor

@buildwithsuhana buildwithsuhana commented Jan 19, 2026

This pull request adds a copy button to the code cells on the keras.io website. It shows a tooltip saying "Copied" when the user clicks on the button and copies the code.

This is how the code cell appears now:
image

After the copy button is clicked:
image

For code cells like below the code output is as shown:

image

Copied content:
layer = Dense(3)
layer.build(input_shape=(None, 3)) # Create the weights of the layer
layer.trainable
layer.trainable_weights
layer.trainable = False
layer.trainable_weights

image

Copied content:
model = Sequential([
ResNet50Base(input_shape=(32, 32, 3), weights='pretrained'),
Dense(10),
])
model.layers[0].trainable = False # Freeze ResNet50Base.

assert model.layers[0].trainable_weights == [] # ResNet50Base has no trainable weights.
assert len(model.trainable_weights) == 2 # Just the bias & kernel of the Dense layer.

model.compile(...)
model.fit(...) # Train Dense while excluding ResNet50Base.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @buildwithsuhana, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the user experience on the Keras.io website by integrating a convenient copy-to-clipboard button directly into all code cells. This feature allows users to effortlessly copy code snippets, streamlining their workflow and enhancing the overall interactivity of the documentation.

Highlights

  • Copy Button Implementation: A new add_copy_buttons_to_code function has been introduced in scripts/autogen_utils.py to dynamically inject a copy button and its container (code__container) into HTML code blocks generated from markdown. This function uses regular expressions to identify <pre> tags and wraps them, while also including logic to skip specific types of code blocks (e.g., Keras model summaries or default code blocks).
  • Styling for Copy Button and Tooltip: New CSS rules have been added to theme/css/docs.css to style the appearance and positioning of the copy button (code__copy--button), its container, and the accompanying 'Copied!' tooltip (code__copy--tooltip). This includes absolute positioning, hover effects, and initial hidden state for the tooltip.
  • JavaScript Copy Logic Enhancement: The theme/js/index.js file has been updated to refine the copy functionality. The code now targets the generic <code> element within the parent container for copying, making it more robust than the previous .language-python selector. Additionally, the tooltip's text dynamically changes to 'Copied!' upon successful copy and reverts after a short delay, providing clear user feedback.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a copy button to code cells, which is a great usability improvement. The implementation is solid. I have a few suggestions to improve maintainability, adhere to modern web standards, and clean up the code. My main points are replacing a deprecated JavaScript API for copying text, ensuring color consistency in the CSS, and simplifying some Python logic.

Copy link
Collaborator

@abheesht17 abheesht17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great! Two comments:

  • Can the copy button be shifted more towards the top-right? The position here looks a bit off:
Image
  • Curious to know how to works for code cells with ">>>". Does it copy the arrows too?
Image

Copy link
Collaborator

@abheesht17 abheesht17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants