File size: 8,502 Bytes
faed411 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import streamlit as st
import random
# Markdown data for insights
markdown_data = {
"python": """
### Python - Top 10 Code Tips ๐
1. **Use virtual environments**: Keep dependencies isolated with `venv` or `conda`. ๐ ๏ธ
2. **Utilize type hints**: Use Python's typing system for better code clarity and maintainability. ๐๏ธ
3. **Leverage list comprehensions**: Write cleaner, more efficient loops. ๐
4. **Avoid mutable default arguments**: Use `None` instead of mutable objects as function defaults. ๐ซ
5. **Use generators for large data**: Save memory with generator expressions and `yield`. ๐ก
6. **Understand the difference between `==` and `is`**: Use `is` for identity checks, `==` for equality. ๐
7. **Use `enumerate()`**: Replace traditional `for` loops when needing indices. ๐งฎ
8. **Try `dataclasses`**: Simplify class creation with Pythonโs `dataclass` decorator. ๐๏ธ
9. **Use `f-strings`**: Format strings efficiently using `f"{value}"`. โจ
10. **Write unit tests**: Test your functions with `unittest` or `pytest`. โ
""",
"streamlit": """
### Streamlit - Top 10 Code Tips ๐๏ธ
1. **Use `st.cache_data` for caching**: Cache expensive operations to speed up your app. โก
2. **Utilize the session state**: Store data between reruns with `st.session_state`. ๐
3. **Leverage components**: Integrate React components with `streamlit-component-template`. ๐งฉ
4. **Write custom widgets**: Expand functionality by creating custom Streamlit widgets. ๐ ๏ธ
5. **Use layout options**: Design better apps with `st.columns` and `st.expander`. ๐
6. **Use interactive sliders**: Create smooth user interactions with `st.slider()`. ๐๏ธ
7. **Handle forms properly**: Use `st.form()` for input forms to optimize reactivity. ๐
8. **Progress feedback**: Show progress with `st.progress()` and `st.spinner()`. ๐
9. **Embed HTML**: Enhance apps with `st.markdown()` using HTML inside markdown. ๐
10. **Deploy easily**: Use `streamlit.io` for one-click deployment. ๐
""",
"typescript": """
### TypeScript - Top 10 Code Tips ๐ป
1. **Use type annotations**: Always annotate variables and function return types. ๐
2. **Prefer interfaces over types**: Interfaces can be extended more easily than types. ๐ช
3. **Use union and intersection types**: Combine types in flexible ways with `|` and `&`. ๐ ๏ธ
4. **Utilize generics**: Write reusable functions and components with generics. ๐
5. **Avoid `any`**: Avoid using `any` type unless absolutely necessary. ๐ซ
6. **Leverage optional chaining**: Safely access deeply nested properties. โ
7. **Use `readonly` for immutability**: Enforce immutability with `readonly`. ๐
8. **Utilize `never` type**: Use `never` for unreachable code paths and exhaustive checks. ๐ง
9. **Understand narrowing**: Use control flow analysis to narrow down types. ๐งฎ
10. **Enable strict mode**: Always turn on strict mode for better type safety. ๐ก๏ธ
""",
"react_native": """
### React Native - Top 10 Code Tips ๐ฑ
1. **Use `StyleSheet`**: Organize and optimize styles with `StyleSheet.create`. ๐จ
2. **Avoid inline styles**: Use stylesheets for reusability and performance. ๐๏ธ
3. **Leverage Flexbox**: Design responsive layouts with Flexbox. ๐
4. **Handle platform-specific code**: Use `Platform` module for OS-specific logic. ๐ ๏ธ
5. **Optimize images**: Use `Image` and `ImageBackground` components efficiently. ๐ผ๏ธ
6. **Utilize hooks**: Use hooks like `useEffect` and `useState` for state management. ๐
7. **Use third-party libraries**: Incorporate libraries like `react-navigation` and `redux`. ๐
8. **Optimize for performance**: Avoid unnecessary re-renders using `React.memo` and `useCallback`. ๐๏ธ
9. **Handle gestures properly**: Use `react-native-gesture-handler` for gesture-based navigation. ๐ฒ
10. **Utilize TypeScript**: Combine React Native with TypeScript for safer code. ๐ป
""",
"libraries": """
### Top 10 Python Libraries for SVG, TypeScript, and React Components ๐ ๏ธ
1. **lxml**: Efficiently parse and manipulate XML, including SVG. ๐
2. **svgwrite**: Easily create and modify SVG files in Python. ๐ผ๏ธ
3. **CairosVG**: Convert SVG files to various formats like PNG. ๐
4. **pydantic**: Handle data validation and complex typing in TypeScript code generation. โ
5. **datamodel-code-generator**: Automatically generate TypeScript code from Python data models. ๐ป
6. **PyV8**: Run JavaScript code from within Python, useful for generating dynamic assets. ๐งโ๐ป
7. **transcrypt**: Compile Python code to JavaScript for use with TypeScript projects. ๐
8. **reactpy**: Build interactive UIs using Python for web and mobile (React-like). ๐ฒ
9. **react-native-svg**: Handle SVG assets in React Native applications. ๐
10. **streamlit-component-template**: Create custom React components inside Streamlit apps. ๐๏ธ
"""
}
# Set up the sidebar
st.sidebar.title("Insights on Technologies ๐ ๏ธ")
sections = ["Python Tips", "Streamlit Tips", "TypeScript Tips", "React Native Tips", "Python Libraries"]
selected_section = st.sidebar.selectbox("Choose a Section:", sections)
# Main content display
if selected_section == "Python Tips":
st.markdown(markdown_data["python"])
elif selected_section == "Streamlit Tips":
st.markdown(markdown_data["streamlit"])
elif selected_section == "TypeScript Tips":
st.markdown(markdown_data["typescript"])
elif selected_section == "React Native Tips":
st.markdown(markdown_data["react_native"])
elif selected_section == "Python Libraries":
st.markdown(markdown_data["libraries"])
# Tabs for interactive code examples
st.subheader("Interactive Code Examples ๐ป")
tabs = st.tabs(["Python", "Streamlit", "TypeScript", "React Native"])
# Example Python code generation
with tabs[0]:
st.markdown("""
**Example: Python Generating TypeScript** ๐จ
```python
def generate_typescript_button():
return '''
import React from 'react';
import { Button } from 'react-native';
interface ButtonProps {
title: string;
onPress: () => void;
}
const CustomButton: React.FC<ButtonProps> = ({ title, onPress }) => (
<Button title={title} onPress={onPress} />
);
export default CustomButton;
'''
```
""")
st.button("Generate TypeScript Code ๐๏ธ")
# Streamlit Example
with tabs[1]:
st.markdown("""
**Example: Custom Streamlit Widget** ๐๏ธ
```python
import streamlit as st
def custom_button(label):
st.write(f"Button: {label}")
if st.button(label):
st.write(f"Button {label} was clicked!")
custom_button("Click Me")
```
""")
st.button("Generate Streamlit Code ๐ป")
# TypeScript Example
with tabs[2]:
st.markdown("""
**Example: TypeScript Component in React Native** ๐ฑ
```typescript
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const HelloWorld = () => (
<View style={styles.container}>
<Text style={styles.text}>Hello, World!</Text>
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 24,
color: 'blue',
},
});
export default HelloWorld;
```
""")
st.button("Generate React Native Code ๐ฒ")
# React Native Example
with tabs[3]:
st.markdown("""
**Example: React Native with TypeScript** ๐ป
```typescript
import React from 'react';
import { Text, View, Button } from 'react-native';
interface AppProps {
title: string;
}
const App: React.FC<AppProps> = ({ title }) => (
<View>
<Text>{title}</Text>
<Button title="Click Me" onPress={() => alert('Button Pressed!')} />
</View>
);
export default App;
```
""")
st.button("Generate Full React Native App ๐ฒ")
# Footer
st.sidebar.info("๐ Enjoy exploring Python, Streamlit, TypeScript, and React Native!")
|